<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Design Pattern | Cying</title><meta name="keywords" content="Java"><meta name="author" content="Cying-承影"><meta name="copyright" content="Cying-承影"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Java 设计模式  一、基础1.1 - 设计模式的重要性  软件工程中，设计模式是对软件设计中普遍存在的各种问题所提出的解决方案。 设计模式用在： 面向对象 (oop) &#x3D;&gt; 功能模块 [ 设计模式 + 算法 ( 数据结构 ) ] &#x3D;&gt; 框架 [ 使用到多种设计模式 ] &#x3D;&gt; 架构 [ 服务器集群 ]   是程序员在编程时应该遵守的原则，也是各种设计模式的基础   1.2 - 设计模式的目的 编">
<meta property="og:type" content="article">
<meta property="og:title" content="Design Pattern">
<meta property="og:url" content="http://cyingk.github.io/2020/01/16/Design_Pattern/index.html">
<meta property="og:site_name" content="Cying">
<meta property="og:description" content="Java 设计模式  一、基础1.1 - 设计模式的重要性  软件工程中，设计模式是对软件设计中普遍存在的各种问题所提出的解决方案。 设计模式用在： 面向对象 (oop) &#x3D;&gt; 功能模块 [ 设计模式 + 算法 ( 数据结构 ) ] &#x3D;&gt; 框架 [ 使用到多种设计模式 ] &#x3D;&gt; 架构 [ 服务器集群 ]   是程序员在编程时应该遵守的原则，也是各种设计模式的基础   1.2 - 设计模式的目的 编">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://www.runoob.com/wp-content/uploads/2014/08/the-relationship-between-design-patterns.jpg">
<meta property="article:published_time" content="2020-01-15T17:23:13.000Z">
<meta property="article:modified_time" content="2020-02-15T18:16:00.000Z">
<meta property="article:author" content="Cying-承影">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://www.runoob.com/wp-content/uploads/2014/08/the-relationship-between-design-patterns.jpg"><link rel="shortcut icon" href="/img/favicon.ico"><link rel="canonical" href="http://cyingk.github.io/2020/01/16/Design_Pattern/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//www.clarity.ms"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>(function(c,l,a,r,i,t,y){
    c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
    t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
    y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "4zndahq92q");</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: 'days',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"Traditional Chinese Activated Manually","cht_to_chs":"Simplified Chinese Activated Manually","day_to_night":"Dark Mode Activated Manually","night_to_day":"Light Mode Activated Manually","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isanchor: false
};

const saveToLocal = {
  // ttl 單位是 天
  set: function setWithExpiry(key, value, ttl) {
    if (ttl === 0) return
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}

// https://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript
const getScript = url => new Promise((resolve, reject) => {
  const script = document.createElement('script')
  script.src = url
  script.async = true
  script.onerror = reject
  script.onload = script.onreadystatechange = function() {
    const loadState = this.readyState
    if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
    script.onload = script.onreadystatechange = null
    resolve()
  }
  document.head.appendChild(script)
})</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-02-16 02:16:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = '1'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}const fontSizeVal = saveToLocal.get('global-font-size')
if (fontSizeVal !== undefined) {
  document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
}})()</script><meta name="generator" content="Hexo 5.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">Loading...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">12</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">Categories</div><div class="length-num">3</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> Categories</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://5b0988e595225.cdn.sohucs.com/images/20190831/84729f5c5bb346208416445feb485df6.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Cying</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> Categories</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Design Pattern</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2020-01-15T17:23:13.000Z" title="Created 2020-01-16 01:23:13">2020-01-16</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2020-02-15T18:16:00.000Z" title="Updated 2020-02-16 02:16:00">2020-02-16</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Note/">Note</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">Word count:</span><span class="word-count">13.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">Reading time:</span><span>47min</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><html><head></head><body><center><h1 style="border:none;">Java 设计模式</h1></center>

<h1 id="一、基础"><a href="#一、基础" class="headerlink" title="一、基础"></a>一、基础</h1><h2 id="1-1-设计模式的重要性"><a href="#1-1-设计模式的重要性" class="headerlink" title="1.1 - 设计模式的重要性"></a>1.1 - 设计模式的重要性</h2><blockquote>
<ul>
<li>软件工程中，设计模式是对软件设计中普遍存在的各种问题所提出的解决方案。</li>
<li>设计模式用在：<ul>
<li>面向对象 (<code>oop</code>) => 功能模块 [ 设计模式 + 算法 ( 数据结构 ) ] => 框架 [ 使用到多种设计模式 ] => 架构 [ 服务器集群 ]</li>
</ul>
</li>
<li>是程序员在编程时应该遵守的原则，也是各种设计模式的基础</li>
</ul>
</blockquote>
<h2 id="1-2-设计模式的目的"><a href="#1-2-设计模式的目的" class="headerlink" title="1.2 - 设计模式的目的"></a>1.2 - 设计模式的目的</h2><blockquote>
<p>编写软件过程中，程序员面临着来自<strong>耦合性</strong>、<strong>内聚性</strong>以及<strong>可维护性</strong>，<strong>可扩展性</strong>，<strong>重用性</strong>，<strong>灵活性</strong>等多方面挑战，设计模式是为了让程序具有更好的性能</p>
<ul>
<li>代码重用性 – 功能相同的代码，不用多次编写</li>
<li>可读性 – 编程规范性，便于其他人阅读和理解</li>
<li>可扩展性 – 当需要增加新的功能时，非常的方便，成为可维护</li>
<li>可靠性 – 当我们增加新的功能后，对原来的功能没有影响</li>
<li>是程序呈现高内聚，低耦合的特性 </li>
</ul>
</blockquote>
<h2 id="1-3-设计模式的七大原则"><a href="#1-3-设计模式的七大原则" class="headerlink" title="1.3 - 设计模式的七大原则"></a>1.3 - 设计模式的七大原则</h2><blockquote>
<p><strong>设计原则的核心思想</strong></p>
<ul>
<li>找出应用中可能需要变化之处，把它们独立出来，不要和那些不需要变化的代码混在一起</li>
<li>针对接口编程，而不是针对实现编程</li>
<li>为了交互对象之间的松耦合设计而努力</li>
</ul>
</blockquote>
<h3 id="1-3-1-单一职责原则"><a href="#1-3-1-单一职责原则" class="headerlink" title="1.3.1 单一职责原则"></a>1.3.1 单一职责原则</h3><blockquote>
<ul>
<li>降低类的复杂度，一个类只负责一项职责</li>
<li>提高类的可读性，可维护性</li>
<li>降低变更引起的风险</li>
<li>通常情况下，我们应当遵守单一职责原则，只有逻辑足够简单，才可以在方法级违反单一职责原则</li>
</ul>
</blockquote>
<p><strong>Demo1</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SRP_Demo01</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		Vehicle SRP = <span class="keyword">new</span> Vehicle();</span><br><span class="line">		SRP.run(<span class="string">"汽车"</span>);</span><br><span class="line">		SRP.run(<span class="string">"轮船"</span>);</span><br><span class="line">		SRP.run(<span class="string">"飞机"</span>);</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Vehicle</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">(String vehicle)</span> </span>{</span><br><span class="line">		System.out.println(vehicle + <span class="string">"在路上跑"</span>);</span><br><span class="line">	}	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>Demo2</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SRP_Demo02</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		RoadVehicle SRP1 = <span class="keyword">new</span> RoadVehicle();</span><br><span class="line">		SRP1.run(<span class="string">"汽车"</span>);</span><br><span class="line">		AirVehicle SRP2 = <span class="keyword">new</span> AirVehicle();</span><br><span class="line">		SRP2.run(<span class="string">"飞机"</span>);</span><br><span class="line">		WaterVehicle SRP3 = <span class="keyword">new</span> WaterVehicle();</span><br><span class="line">		SRP3.run(<span class="string">"轮船"</span>);</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RoadVehicle</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">(String vehicle)</span> </span>{</span><br><span class="line">		System.out.println(vehicle + <span class="string">"在路上跑"</span>);</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AirVehicle</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">(String vehicle)</span> </span>{</span><br><span class="line">		System.out.println(vehicle + <span class="string">"在天上飞"</span>);</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">WaterVehicle</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">(String vehicle)</span> </span>{</span><br><span class="line">		System.out.println(vehicle + <span class="string">"在水里游"</span>);</span><br><span class="line">	}	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>Demo3</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SRP_Demo03</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		Vehicle SRP = <span class="keyword">new</span> Vehicle();</span><br><span class="line">		SRP.roadVehicle(<span class="string">"汽车"</span>);</span><br><span class="line">		SRP.airVehicle(<span class="string">"飞机"</span>);</span><br><span class="line">		SRP.waterVehicle(<span class="string">"轮船"</span>);</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Vehicle</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">roadVehicle</span><span class="params">(String vehicle)</span> </span>{</span><br><span class="line">		System.out.println(vehicle + <span class="string">"在路上跑"</span>);</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">airVehicle</span><span class="params">(String vehicle)</span> </span>{</span><br><span class="line">		System.out.println(vehicle + <span class="string">"在天上飞"</span>);</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">waterVehicle</span><span class="params">(String vehicle)</span> </span>{</span><br><span class="line">		System.out.println(vehicle + <span class="string">"在水里游"</span>);</span><br><span class="line">	}	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="1-3-2-接口隔离原则"><a href="#1-3-2-接口隔离原则" class="headerlink" title="1.3.2 接口隔离原则"></a>1.3.2 接口隔离原则</h3><blockquote>
<ul>
<li>客户端不应该依赖它不需要的接口，即一个类对另一个类的依赖应该建立在最小的接口上</li>
</ul>
</blockquote>
<p><strong>Demo1</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ISP_Demo01</span> </span>{</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">Int1</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">fun1</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">fun2</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">fun3</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">fun4</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">fun5</span><span class="params">()</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{	<span class="comment">// A 通过接口依赖 B ，但只用1，2，3</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">depend1</span><span class="params">(Int1 i)</span> </span>{</span><br><span class="line">		i.fun1();</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">depend2</span><span class="params">(Int1 i)</span> </span>{</span><br><span class="line">		i.fun2();</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">depend3</span><span class="params">(Int1 i)</span> </span>{</span><br><span class="line">		i.fun3();</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span> <span class="keyword">implements</span> <span class="title">Int1</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun1</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"B 实现了 fun1"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun2</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"B 实现了 fun2"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun3</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"B 实现了 fun3"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun4</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"B 实现了 fun4"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun5</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"B 实现了 fun5"</span>);</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">C</span> </span>{	<span class="comment">// A 通过接口依赖D ，但只用1，2，3</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">depend1</span><span class="params">(Int1 i)</span> </span>{</span><br><span class="line">		i.fun1();</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">depend4</span><span class="params">(Int1 i)</span> </span>{</span><br><span class="line">		i.fun4();</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">depend5</span><span class="params">(Int1 i)</span> </span>{</span><br><span class="line">		i.fun5();</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">D</span> <span class="keyword">implements</span> <span class="title">Int1</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun1</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"D 实现了 fun1"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun2</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"D 实现了 fun2"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun3</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"D 实现了 fun3"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun4</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"D 实现了 fun4"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun5</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"D 实现了 fun5"</span>);</span><br><span class="line">	}	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>Demo2</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ISP_Demo02</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		A a = <span class="keyword">new</span> A();</span><br><span class="line">		a.int1(<span class="keyword">new</span> B());</span><br><span class="line">		a.int2(<span class="keyword">new</span> B());</span><br><span class="line">		a.int3(<span class="keyword">new</span> B());</span><br><span class="line">		C c = <span class="keyword">new</span> C();</span><br><span class="line">		c.int1(<span class="keyword">new</span> D());</span><br><span class="line">		c.int4(<span class="keyword">new</span> D());</span><br><span class="line">		c.int5(<span class="keyword">new</span> D());</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">int1</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">fun1</span><span class="params">()</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">int2</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">fun2</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">fun3</span><span class="params">()</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">int3</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">fun4</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">fun5</span><span class="params">()</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">int1</span><span class="params">(int1 i)</span> </span>{</span><br><span class="line">		i.fun1();</span><br><span class="line">	}	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">int2</span><span class="params">(int2 i)</span> </span>{</span><br><span class="line">		i.fun2();</span><br><span class="line">	}	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">int3</span><span class="params">(int2 i)</span> </span>{</span><br><span class="line">		i.fun3();</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span> <span class="keyword">implements</span> <span class="title">int1</span>, <span class="title">int2</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun1</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"B 实现了 fun1"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun2</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"B 实现了 fun2"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun3</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"B 实现了 fun3"</span>);</span><br><span class="line">	}		</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">C</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">int1</span><span class="params">(int1 i)</span> </span>{</span><br><span class="line">		i.fun1();</span><br><span class="line">	}	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">int4</span><span class="params">(int3 i)</span> </span>{</span><br><span class="line">		i.fun4();</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">int5</span><span class="params">(int3 i)</span> </span>{</span><br><span class="line">		i.fun5();</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">D</span> <span class="keyword">implements</span> <span class="title">int1</span>, <span class="title">int3</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun1</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"D 实现了 fun1"</span>);</span><br><span class="line">	}	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun4</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"D 实现了 fun4"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun5</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"D 实现了 fun5"</span>);</span><br><span class="line">	}	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="1-3-3-依赖倒转原则"><a href="#1-3-3-依赖倒转原则" class="headerlink" title="1.3.3 依赖倒转原则"></a>1.3.3 依赖倒转原则</h3><blockquote>
<ul>
<li>高层模块不应该依赖底层模块，二者都应该依赖其抽象</li>
<li>抽象不应该依赖细节，细节应依赖抽象</li>
<li>依赖倒转的中心思想是面向接口编程</li>
<li>相对于细节的多变性，抽象的东西要稳定的多。以抽象为基础搭建的架构比以细节为基础的架构要稳定得多，在<code>Java</code>中，抽象指的是接口或抽象类，细节就是指具体的实现类</li>
<li>使用接口或者抽象类的目的是指定好规范，而不涉及任何具体的操作，把展现细节的任务交给实现类</li>
</ul>
</blockquote>
<blockquote>
<p><strong>依赖关系传递的三种方式和应用案例</strong></p>
<ul>
<li>接口传递</li>
<li>构造方法传递</li>
<li><code>setter</code> 方式传递</li>
</ul>
</blockquote>
<blockquote>
<p><strong>注意事项和细节</strong></p>
<ul>
<li>低层模块尽量都要有抽象类或接口，或者两者都有，程序稳定性良好</li>
<li>变量的声明类型尽量使抽象类或接口，这样我们的变量引用和实际对象间，就存在一个缓冲层，利于程序扩展和优化</li>
<li>继承时遵循里氏替换原则</li>
</ul>
</blockquote>
<p><strong>Demo1</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DIP_Demo01</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		Person person = <span class="keyword">new</span> Person();</span><br><span class="line">		person.receive(<span class="keyword">new</span> Email());</span><br><span class="line">		</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Email</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getInfo</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Hello World"</span>;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">receive</span><span class="params">(Email email)</span> </span>{</span><br><span class="line">		System.out.println(email.getInfo());</span><br><span class="line">	}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>Demo2</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DIP_Demo02</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		Person person = <span class="keyword">new</span> Person();</span><br><span class="line">		person.receive(<span class="keyword">new</span> Email());</span><br><span class="line">		person.receive(<span class="keyword">new</span> Wechat());		</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">IReceiver</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getInfo</span><span class="params">()</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Email</span> <span class="keyword">implements</span> <span class="title">IReceiver</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getInfo</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Hello Email"</span>;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Wechat</span> <span class="keyword">implements</span> <span class="title">IReceiver</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getInfo</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Hello Wechat"</span>;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">receive</span><span class="params">(IReceiver receiver)</span> </span>{</span><br><span class="line">		System.out.println(receiver.getInfo());</span><br><span class="line">	}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="1-3-4-里氏替换原则"><a href="#1-3-4-里氏替换原则" class="headerlink" title="1.3.4 里氏替换原则"></a>1.3.4 里氏替换原则</h3><blockquote>
<ul>
<li>继承包含这样一层含义：父类中凡是已经实现好的方法，实际上是在设定规范和契约，虽然它不强制要求所有的子类必须遵循这些契约，但如果子类对这些已经实现的方法任意修改，就会对整个继承体系造成破坏</li>
<li>继承在程序设计带来便利的同时也带来了弊端，比如使用继承会给程序带来侵入性，程序的可移植性降低，增加对象间的耦合性，如果一个类被其他类所继承，则当这个类需要修改时，必须考虑到所有的子类，并且父类修改后，所有涉及到子类的功能都有可能产生故障</li>
</ul>
</blockquote>
<blockquote>
<ul>
<li>如果对每个类型为<code>T1</code>的对象<code>O1</code>，都有类型为<code>T2</code>的对象<code>O2</code>，使得以<code>T1</code>定义的所有程序<code>P</code>在所有的对象<code>O1</code>都代换成<code>O2</code>时，程序<code>P</code>的行为没有发生变化，那么类型<code>T2</code>是类型<code>T1</code>的子类型。换句话来说，所有引用基类的地方必须能透明的使用其子类的对象</li>
<li>在使用继承时，在子类中尽量不要重写父类的方法</li>
<li>里氏替换原则告诉我们，继承实际上让两个类耦合性增强了，在适当的情况下，可以通过<strong>聚合、组合、依赖来解决问题</strong></li>
</ul>
</blockquote>
<p><strong>Demo1</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LSP_Demo01</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		A a = <span class="keyword">new</span> A();</span><br><span class="line">		System.out.println(<span class="string">"11-3="</span> + a.fun1(<span class="number">11</span>, <span class="number">3</span>));</span><br><span class="line">		System.out.println(<span class="string">"------------------"</span>);</span><br><span class="line">		B b = <span class="keyword">new</span> B();</span><br><span class="line">		System.out.println(<span class="string">"11-3="</span> + b.fun1(<span class="number">11</span>, <span class="number">3</span>));</span><br><span class="line">		System.out.println(<span class="string">"11+3+9="</span> + b.fun2(<span class="number">11</span>, <span class="number">3</span>));</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">fun1</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> num1 - num2;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span> <span class="keyword">extends</span> <span class="title">A</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">fun1</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> num1 + num2;</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">fun2</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> fun1(num1, num2)+<span class="number">9</span>;</span><br><span class="line">	}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>Demo2</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LSP_Demo02</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		A a = <span class="keyword">new</span> A();</span><br><span class="line">		System.out.println(<span class="string">"11-3="</span> + a.fun1(<span class="number">11</span>, <span class="number">3</span>));</span><br><span class="line">		System.out.println(<span class="string">"------------------"</span>);</span><br><span class="line">		B b = <span class="keyword">new</span> B();</span><br><span class="line">		System.out.println(<span class="string">"11+3="</span> + b.fun1(<span class="number">11</span>, <span class="number">3</span>));</span><br><span class="line">		System.out.println(<span class="string">"11+3+9="</span> + b.fun2(<span class="number">11</span>, <span class="number">3</span>));</span><br><span class="line">		System.out.println(<span class="string">"11-3="</span> + b.fun3(<span class="number">11</span>, <span class="number">3</span>));</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Base</span> </span>{	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> <span class="keyword">extends</span> <span class="title">Base</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">fun1</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> num1 - num2;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span> <span class="keyword">extends</span> <span class="title">Base</span> </span>{</span><br><span class="line">	</span><br><span class="line">	A a = <span class="keyword">new</span> A();</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">fun1</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> num1 + num2;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">fun2</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> fun1(num1, num2)+<span class="number">9</span>;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">fun3</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> a.fun1(num1, num2);</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="1-3-5-开闭原则"><a href="#1-3-5-开闭原则" class="headerlink" title="1.3.5 开闭原则"></a>1.3.5 开闭原则</h3><blockquote>
<ul>
<li>一个软件实体，如类、模块、函数，应该对扩展开放，对修改关闭。用抽象构建框架，用实现扩展细节</li>
<li>当软件需要变化时，尽量通过扩展软件实体的行为来实现变化，而不是通过修改已有的代码来实现变化</li>
<li>编程中遵循其他原则，以及使用设计模式的目的就是遵循开闭原则</li>
</ul>
</blockquote>
<p><strong>Demo1</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OCP_Demo01</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		GraphicEditor graphicEditor = <span class="keyword">new</span> GraphicEditor();</span><br><span class="line">		graphicEditor.drawRectangle(<span class="keyword">new</span> Rectangle());</span><br><span class="line">		graphicEditor.drawCircle(<span class="keyword">new</span> Circle());</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Shape</span> </span>{</span><br><span class="line">	<span class="keyword">int</span> m_type;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">GraphicEditor</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">drawShape</span><span class="params">(Shape s)</span> </span>{</span><br><span class="line">		<span class="keyword">if</span> (s.m_type==<span class="number">1</span>) {</span><br><span class="line">			drawRectangle(s);</span><br><span class="line">		} <span class="keyword">else</span> <span class="keyword">if</span> (s.m_type==<span class="number">2</span>) {</span><br><span class="line">			drawCircle(s);</span><br><span class="line">		}</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">drawRectangle</span><span class="params">(Shape s)</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"绘制矩形"</span>);</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">drawCircle</span><span class="params">(Shape s)</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"绘制圆形"</span>);</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Rectangle</span> <span class="keyword">extends</span> <span class="title">Shape</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Rectangle</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">super</span>.m_type = <span class="number">1</span>;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Circle</span> <span class="keyword">extends</span> <span class="title">Shape</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Circle</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">super</span>.m_type = <span class="number">1</span>;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果要继续增加图形，就要改动多处</span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>Demo2</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OCP_Demo02</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		GraphicEditor graphicEditor = <span class="keyword">new</span> GraphicEditor();</span><br><span class="line">		graphicEditor.drawShape(<span class="keyword">new</span> Rectangle());</span><br><span class="line">		graphicEditor.drawShape(<span class="keyword">new</span> Circle());</span><br><span class="line">		graphicEditor.drawShape(<span class="keyword">new</span> Triangle());</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Shape</span> </span>{</span><br><span class="line">	<span class="keyword">int</span> m_type;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">draw</span><span class="params">()</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">GraphicEditor</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">drawShape</span><span class="params">(Shape s)</span> </span>{</span><br><span class="line">		s.draw();</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Rectangle</span> <span class="keyword">extends</span> <span class="title">Shape</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Rectangle</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">super</span>.m_type = <span class="number">1</span>;</span><br><span class="line">	}</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">draw</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"绘制矩形"</span>);</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Circle</span> <span class="keyword">extends</span> <span class="title">Shape</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Circle</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">super</span>.m_type = <span class="number">1</span>;</span><br><span class="line">	}	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">draw</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"绘制圆形"</span>);</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Triangle</span> <span class="keyword">extends</span> <span class="title">Shape</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Triangle</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">super</span>.m_type = <span class="number">1</span>;</span><br><span class="line">	}	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">draw</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"绘制三角形"</span>);</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果要继续增加图形，就只要新增类，新增调用，不需要对已有的代码改动</span></span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="1-3-6-迪米特原则"><a href="#1-3-6-迪米特原则" class="headerlink" title="1.3.6 迪米特原则"></a>1.3.6 迪米特原则</h3><blockquote>
<ul>
<li>一个对象应该是对其他对象保持最少的了解</li>
<li>类与类关系越密切，耦合度越大</li>
<li>迪米特原则又交<strong>最少知道原则</strong>，即一个类对自己依赖的类知道的越少越好。也就是说，对于被依赖的类，不管多么复杂，都尽量将逻辑封装在类的内部，对外除了提供 <code>public</code> 方法，不对外泄露任何信息</li>
<li>只与直接的朋友通信<ul>
<li>直接的朋友 – 每个对象都会与其他对象有耦合关系，只要两个对象之间有耦合关系，我们就称这两个对象之间是朋友关系。耦合的方式很多，依赖、关联、组合、聚合等。其中，我们称出现成员变量，方法参数，方法返回值中的类为直接的朋友，而出现在局部变量中的类不是直接的朋友。也就是说，陌生的类最好不要以局部变量的形式出现在类的内部</li>
</ul>
</li>
</ul>
</blockquote>
<blockquote>
<p><strong>注意事项及细节</strong></p>
<ul>
<li>迪米特原则的核心是降低类之间的耦合</li>
<li>由于每个类都减少了不必要的依赖，因此迪米特原则只是要求降低类间的耦合关系，并不是完全没有依赖</li>
</ul>
</blockquote>
<p><strong>Demo1</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DP_Demo01</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		SchoolManager sManager = <span class="keyword">new</span> SchoolManager();</span><br><span class="line">		sManager.printAllEmployee(<span class="keyword">new</span> CollegeManager());</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Employee</span> </span>{</span><br><span class="line">	<span class="keyword">private</span> String id;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(String id)</span> </span>{</span><br><span class="line">		<span class="keyword">this</span>.id = id;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>.id;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CollegeEmployee</span> </span>{</span><br><span class="line">	<span class="keyword">private</span> String id;</span><br><span class="line">  </span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(String id)</span> </span>{</span><br><span class="line">		<span class="keyword">this</span>.id = id;</span><br><span class="line">	}	</span><br><span class="line">  </span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>.id;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CollegeManager</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> List<collegeemployee> <span class="title">getAllEmployee</span><span class="params">()</span> </collegeemployee></span>{</span><br><span class="line">		List<collegeemployee> list = <span class="keyword">new</span> ArrayList<collegeemployee>();</collegeemployee></collegeemployee></span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {</span><br><span class="line">			CollegeEmployee employee = <span class="keyword">new</span> CollegeEmployee();</span><br><span class="line">			employee.setId(<span class="string">"学院员工id = "</span> + i);</span><br><span class="line">			list.add(employee);</span><br><span class="line">		}</span><br><span class="line">		<span class="keyword">return</span> list;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> NLM13</span></span><br><span class="line"><span class="comment"> * SchoolManager 的直接朋友类：Employee（返回值）、CollegeManager（参数）</span></span><br><span class="line"><span class="comment"> * CollegeEmployee 不是直接朋友类，而是一个陌生类，违反迪米特原则，陌生类最好不要以局部变量的形式出现</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SchoolManager</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> List<employee> <span class="title">getAllEmployee</span><span class="params">()</span> </employee></span>{		</span><br><span class="line">		List<employee> list = <span class="keyword">new</span> ArrayList<employee>();		</employee></employee></span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">5</span>; i++) {</span><br><span class="line">			Employee employee = <span class="keyword">new</span> Employee();</span><br><span class="line">			employee.setId(<span class="string">"学校总部员工id = "</span> + i);</span><br><span class="line">			list.add(employee);</span><br><span class="line">		}		</span><br><span class="line">		<span class="keyword">return</span> list;		</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">printAllEmployee</span><span class="params">(CollegeManager sub)</span> </span>{		</span><br><span class="line">    <span class="comment">//这里的CollegeEmployee是陌生类，不应该出现在这里</span></span><br><span class="line">		List<collegeemployee> list1 = sub.getAllEmployee();</collegeemployee></span><br><span class="line">		System.out.println(<span class="string">"-----------学院员工-----------"</span>);</span><br><span class="line">		<span class="keyword">for</span> (CollegeEmployee e : list1) {</span><br><span class="line">			System.out.println(e.getId());</span><br><span class="line">		}		</span><br><span class="line">		List<employee> list2 = <span class="keyword">this</span>.getAllEmployee();</employee></span><br><span class="line">		System.out.println(<span class="string">"-----------总部员工-----------"</span>);</span><br><span class="line">		<span class="keyword">for</span> (Employee e : list2) {</span><br><span class="line">			System.out.println(e.getId());</span><br><span class="line">		}		</span><br><span class="line">	}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>Demo2</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DP_Demo02</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		SchoolManager sManager = <span class="keyword">new</span> SchoolManager();</span><br><span class="line">		sManager.printAllEmployee(<span class="keyword">new</span> CollegeManager());</span><br><span class="line">	}	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Employee</span> </span>{</span><br><span class="line">	<span class="keyword">private</span> String id;	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(String id)</span> </span>{</span><br><span class="line">		<span class="keyword">this</span>.id = id;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>.id;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CollegeEmployee</span> </span>{</span><br><span class="line">	<span class="keyword">private</span> String id;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(String id)</span> </span>{</span><br><span class="line">		<span class="keyword">this</span>.id = id;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>.id;</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CollegeManager</span> </span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> List<collegeemployee> <span class="title">getAllEmployee</span><span class="params">()</span> </collegeemployee></span>{</span><br><span class="line">		List<collegeemployee> list = <span class="keyword">new</span> ArrayList<collegeemployee>();</collegeemployee></collegeemployee></span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {</span><br><span class="line">			CollegeEmployee employee = <span class="keyword">new</span> CollegeEmployee();</span><br><span class="line">			employee.setId(<span class="string">"学院员工id = "</span> + i);</span><br><span class="line">			list.add(employee);</span><br><span class="line">		}</span><br><span class="line">		<span class="keyword">return</span> list;</span><br><span class="line">	}</span><br><span class="line">  </span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">printEmployee</span><span class="params">()</span> </span>{</span><br><span class="line">		List<collegeemployee> list1 = <span class="keyword">this</span>.getAllEmployee();</collegeemployee></span><br><span class="line">		System.out.println(<span class="string">"-----------学院员工-----------"</span>);</span><br><span class="line">		<span class="keyword">for</span> (CollegeEmployee e : list1) {</span><br><span class="line">			System.out.println(e.getId());</span><br><span class="line">		}</span><br><span class="line">	}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SchoolManager</span> </span>{	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> List<employee> <span class="title">getAllEmployee</span><span class="params">()</span> </employee></span>{		</span><br><span class="line">		List<employee> list = <span class="keyword">new</span> ArrayList<employee>();		</employee></employee></span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">5</span>; i++) {</span><br><span class="line">			Employee employee = <span class="keyword">new</span> Employee();</span><br><span class="line">			employee.setId(<span class="string">"学校总部员工id = "</span> + i);</span><br><span class="line">			list.add(employee);</span><br><span class="line">		}		</span><br><span class="line">		<span class="keyword">return</span> list;		</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">printAllEmployee</span><span class="params">(CollegeManager sub)</span> </span>{</span><br><span class="line">				</span><br><span class="line">		<span class="comment">// 将原有的学院输出，转放到了自己的类里</span></span><br><span class="line">		sub.printEmployee();	</span><br><span class="line">		</span><br><span class="line">		List<employee> list2 = <span class="keyword">this</span>.getAllEmployee();</employee></span><br><span class="line">		System.out.println(<span class="string">"-----------总部员工-----------"</span>);</span><br><span class="line">		<span class="keyword">for</span> (Employee e : list2) {</span><br><span class="line">			System.out.println(e.getId());</span><br><span class="line">		}		</span><br><span class="line">	}	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="1-3-7-合成复用原则"><a href="#1-3-7-合成复用原则" class="headerlink" title="1.3.7 合成复用原则"></a>1.3.7 合成复用原则</h3><blockquote>
<ul>
<li>原则是尽量使用合成/聚合的方式，而不是使用继承</li>
<li>如果只是想让 B 类去使用 A 类的方法，使用继承就会让 B 和 A 的耦合性增强</li>
<li>依赖 – B 类中有方法的参数是 A 类型</li>
<li>聚合 – B 类中有 A 类的属性，通过 setter 设置 A 类的值</li>
<li>组合 – B 类中有 A 类的属性，在创建 B 的时候直接实例化 A 的值</li>
</ul>
</blockquote>
<h2 id="1-4-UML-类图"><a href="#1-4-UML-类图" class="headerlink" title="1.4 - UML 类图"></a>1.4 - UML 类图</h2><blockquote>
<p><strong>基本介绍</strong></p>
<ul>
<li><code>UML (Unified modeling language)</code> – 统一建模语言，是一种用于软件系统分析和设计的语言工具，用于帮助软件开发人员进行思考和记录思路的结果。</li>
<li>本身是一套符号的规定，就像数学符号和化学符号一样，这些符号用于描述软件模型中的各个元素和他们之间关系，比如类、接口、实现、泛化、依赖、组合、聚合等</li>
<li>使用<code>UML</code>来建模，常用的工具有<code>Rational Rose</code>，也可以用一些插件来建模</li>
</ul>
</blockquote>
<blockquote>
<ul>
<li><code>Dependency</code> – 依赖</li>
<li><code>Association</code> – 关联</li>
<li><code>Generalization</code> – 泛化/继承</li>
<li><code>Realization</code> – 实现</li>
<li><code>Aggregation</code> – 聚合</li>
<li><code>Composite</code> – 组合</li>
</ul>
</blockquote>
<h1 id="二、单例模式"><a href="#二、单例模式" class="headerlink" title="二、单例模式"></a>二、单例模式</h1><blockquote>
<p>采取一定的方法，保证在整个软件系统中，对某个类<strong>只能存在一个对象实例</strong>，并且该类只提供一个取得其对象实例的方法</p>
</blockquote>
<h2 id="2-1-源码追溯"><a href="#2-1-源码追溯" class="headerlink" title="2.1 - 源码追溯"></a>2.1 - 源码追溯</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Runtime</span> </span>{</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Runtime currentRuntime = <span class="keyword">new</span> Runtime();</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Runtime <span class="title">getRuntime</span><span class="params">()</span> </span>{</span><br><span class="line">    <span class="keyword">return</span> currentRuntime;</span><br><span class="line">  }</span><br><span class="line">}</span><br><span class="line"><span class="comment">// 饿汉式 -- 静态常量</span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="2-2-注意事项和细节说明"><a href="#2-2-注意事项和细节说明" class="headerlink" title="2.2 - 注意事项和细节说明"></a>2.2 - 注意事项和细节说明</h2><blockquote>
<ul>
<li>单例模式保证了系统内存中该类只存在一个对象，节省了系统资源，对于一些需要频繁创建销毁的对象，使用单例模式可以提高系统性能</li>
<li>当想实例化一个单例类的时候，必须要记住使用相应的获取对象的方法，而不是使用 <code>new</code></li>
<li>单例模式使用的场景：<ul>
<li>需要频繁的进行创建和销毁的对象</li>
<li>创建对象时耗时过多或耗费资源过多，但又经常要用到的对象</li>
<li>工具类对象</li>
<li>频繁访问数据库或文件的对象</li>
</ul>
</li>
</ul>
</blockquote>
<h2 id="2-3-八种方式"><a href="#2-3-八种方式" class="headerlink" title="2.3 - 八种方式"></a>2.3 - 八种方式</h2><h3 id="2-3-1-饿汉式-静态常量"><a href="#2-3-1-饿汉式-静态常量" class="headerlink" title="2.3.1 饿汉式 - 静态常量"></a>2.3.1 饿汉式 - 静态常量</h3><blockquote>
<ul>
<li>构造器私有化（防止 new ）</li>
<li>类的内部创建对象</li>
<li>向外暴露一个静态方法</li>
<li>代码实现</li>
</ul>
</blockquote>
<p><strong>优点</strong></p>
<blockquote>
<p>这种写法比较简单，就是在类加载的时候就完成实例化。避免了线程同步的问题</p>
</blockquote>
<p><strong>缺点</strong></p>
<blockquote>
<p>在类装载的时候就完成实例化，没有达到 <code>Lazy Loading</code> 的效果。如果从始至终从未使用过这个实例，则会造成内存的浪费</p>
</blockquote>
<p><strong>总结</strong></p>
<blockquote>
<p>这种单例模式可用，可能造成内存浪费</p>
</blockquote>
<p><strong>Demo</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">STtest</span> </span>{</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"></span><br><span class="line">		Singleton singleton1 = Singleton.getInstance();</span><br><span class="line">		Singleton singleton2 = singleton1.getInstance();</span><br><span class="line">		System.out.println(<span class="string">"singleton1.hashCode: "</span> + singleton1.hashCode());</span><br><span class="line">		System.out.println(<span class="string">"singleton2.hashCode: "</span> + singleton2.hashCode());</span><br><span class="line">    <span class="comment">// 结果是两者的 hashCode 的值一样</span></span><br><span class="line">		</span><br><span class="line">	}	</span><br><span class="line">	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Singleton</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 构造器私有化，外部不能 new </span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Singleton</span><span class="params">()</span> </span>{</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 本类内部创建对象实例</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">static</span> Singleton instance = <span class="keyword">new</span> Singleton();</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 提供一个共有的静态方法，返回实例对象</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Singleton <span class="title">getInstance</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> instance;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="2-3-2-饿汉式-静态代码块"><a href="#2-3-2-饿汉式-静态代码块" class="headerlink" title="2.3.2 饿汉式 - 静态代码块"></a>2.3.2 饿汉式 - 静态代码块</h3><p><strong>优缺点</strong></p>
<blockquote>
<ul>
<li>这种方式和<strong>静态常量</strong>方式其实类似，只不过将类实例化的过程放在了静态代码块中，也是在类装载的时候，就执行静态代码块中的代码，初始化类的实例。优缺点和上面是一样的</li>
<li><strong>结论</strong> – 这种单例模式可用，可能会造成内存浪费</li>
</ul>
</blockquote>
<p><strong>Demo</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">STtest</span> </span>{</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"></span><br><span class="line">		Singleton singleton1 = Singleton.getInstance();</span><br><span class="line">		Singleton singleton2 = singleton1.getInstance();</span><br><span class="line">		System.out.println(<span class="string">"singleton1.hashCode: "</span> + singleton1.hashCode());</span><br><span class="line">		System.out.println(<span class="string">"singleton2.hashCode: "</span> + singleton2.hashCode());</span><br><span class="line">    <span class="comment">// 结果是两者的 hashCode 的值一样</span></span><br><span class="line">		</span><br><span class="line">	}	</span><br><span class="line">	</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Singleton</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 构造器私有化，外部不能 new </span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Singleton</span><span class="params">()</span> </span>{</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 本类内部创建对象实例</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> Singleton instance;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">static</span> {	<span class="comment">// 在静态代码块中，创建单例对象</span></span><br><span class="line">		instance = <span class="keyword">new</span> Singleton();</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 提供一个共有的静态方法，返回实例对象</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Singleton <span class="title">getInstance</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> instance;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="2-3-3-懒汉式-–-线程不安全"><a href="#2-3-3-懒汉式-–-线程不安全" class="headerlink" title="2.3.3 懒汉式 – 线程不安全"></a>2.3.3 懒汉式 – 线程不安全</h3><p><strong>优缺点</strong></p>
<blockquote>
<ul>
<li>起到了 <code>Lazy Loading</code> 的效果，但只能在单线程下使用</li>
<li>如果在多线程下，一个线程进入了判空，还未来得及往下执行，另一个线程也通过了这个判断语句，这时便会产生多个实例，所以在多线程环境下不可使用这种方式</li>
<li><strong>结论</strong> – 在实际开发中，不要使用这种方式</li>
</ul>
</blockquote>
<p><strong>Demo</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">STtest</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		</span><br><span class="line">		SingleTon singleTon1 = SingleTon.getInstance();</span><br><span class="line">		SingleTon singleTon2 = singleTon1.getInstance();</span><br><span class="line">		System.out.println(<span class="string">"singleton1.hashCode: "</span> + singleTon1.hashCode());</span><br><span class="line">		System.out.println(<span class="string">"singleton2.hashCode: "</span> + singleTon2.hashCode());</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SingleTon</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> SingleTon instance;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="title">SingleTon</span><span class="params">()</span> </span>{</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 提供一个静态方法</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> SingleTon <span class="title">getInstance</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">if</span> (instance==<span class="keyword">null</span>) {</span><br><span class="line">			instance = <span class="keyword">new</span> SingleTon();</span><br><span class="line">		}</span><br><span class="line">		<span class="keyword">return</span> instance;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="2-3-4-懒汉式-–-线程安全，同步方法"><a href="#2-3-4-懒汉式-–-线程安全，同步方法" class="headerlink" title="2.3.4 懒汉式 – 线程安全，同步方法"></a>2.3.4 懒汉式 – 线程安全，同步方法</h3><p><strong>优缺点</strong></p>
<blockquote>
<ul>
<li>解决了线程不安全问题</li>
<li>效率太低了，每个线程在想获得类的实例的时候，执行获取方法都要进行同步。而其实这个方法只执行一次实例化代码就够了，后面想获得该类实例直接 <code>return</code> 就好，方法进行同步效率太低</li>
<li><strong>结论</strong> – 在实际开发中，不推荐使用这种方式</li>
</ul>
</blockquote>
<p><strong>Demo</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">STtest</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		</span><br><span class="line">		SingleTon singleTon1 = SingleTon.getInstance();</span><br><span class="line">		SingleTon singleTon2 = singleTon1.getInstance();</span><br><span class="line">		System.out.println(<span class="string">"singleton1.hashCode: "</span> + singleTon1.hashCode());</span><br><span class="line">		System.out.println(<span class="string">"singleton2.hashCode: "</span> + singleTon2.hashCode());</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SingleTon</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> SingleTon instance;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="title">SingleTon</span><span class="params">()</span> </span>{</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 加入同步处理的代码，解决线程安全的问题</span></span><br><span class="line">	<span class="comment">// 提供一个静态方法</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">synchronized</span> SingleTon <span class="title">getInstance</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">if</span> (instance==<span class="keyword">null</span>) {</span><br><span class="line">			instance = <span class="keyword">new</span> SingleTon();</span><br><span class="line">		}</span><br><span class="line">		<span class="keyword">return</span> instance;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="2-3-5-懒汉式-–-线程安全，同步代码块"><a href="#2-3-5-懒汉式-–-线程安全，同步代码块" class="headerlink" title="2.3.5 懒汉式 – 线程安全，同步代码块"></a>2.3.5 懒汉式 – 线程安全，同步代码块</h3><p><strong>优缺点</strong></p>
<blockquote>
<ul>
<li>本意是想对第四种方式的改进，因为前面同步方法效率太低，改进为同步产生实例化的代码块</li>
<li><strong>但这种方法并不能起到线程同步的作用</strong>。原因和第三种方式是一样的</li>
<li><strong>结论</strong> – 在实际开发中，不能使用这种方式</li>
</ul>
</blockquote>
<p><strong>Demo</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">STtest</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		</span><br><span class="line">		SingleTon singleTon1 = SingleTon.getInstance();</span><br><span class="line">		SingleTon singleTon2 = singleTon1.getInstance();</span><br><span class="line">		System.out.println(<span class="string">"singleton1.hashCode: "</span> + singleTon1.hashCode());</span><br><span class="line">		System.out.println(<span class="string">"singleton2.hashCode: "</span> + singleTon2.hashCode());</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SingleTon</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> SingleTon instance;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="title">SingleTon</span><span class="params">()</span> </span>{</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 加入同步处理的代码，解决线程安全的问题</span></span><br><span class="line">	<span class="comment">// 提供一个静态方法</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> SingleTon <span class="title">getInstance</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">if</span> (instance==<span class="keyword">null</span>) {</span><br><span class="line">			<span class="keyword">synchronized</span> (SingleTon<span class="class">.<span class="keyword">class</span>)</span>{</span><br><span class="line">				instance = <span class="keyword">new</span> SingleTon();</span><br><span class="line">			}</span><br><span class="line">		}</span><br><span class="line">		<span class="keyword">return</span> instance;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="2-3-6-双重检查"><a href="#2-3-6-双重检查" class="headerlink" title="2.3.6 双重检查"></a>2.3.6 双重检查</h3><blockquote>
<ul>
<li>是在多线程开发中常用到的</li>
<li>避免反复进行同步方法</li>
<li>线程安全，延迟加载，效率较高</li>
<li><strong>结论</strong> – 在实际开发中，推荐使用这种单例设计模式</li>
</ul>
</blockquote>
<p><strong>Demo</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">STtest</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		</span><br><span class="line">		SingleTon singleTon1 = SingleTon.getInstance();</span><br><span class="line">		SingleTon singleTon2 = singleTon1.getInstance();</span><br><span class="line">		System.out.println(<span class="string">"singleton1.hashCode: "</span> + singleTon1.hashCode());</span><br><span class="line">		System.out.println(<span class="string">"singleton2.hashCode: "</span> + singleTon2.hashCode());</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SingleTon</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// valatile，共享变量的值，一旦修改，就会立马更新</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">volatile</span> SingleTon instance;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="title">SingleTon</span><span class="params">()</span> </span>{</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 双重检查代码</span></span><br><span class="line">	<span class="comment">// 加入同步处理的代码，解决线程安全的问题</span></span><br><span class="line">	<span class="comment">// 提供一个静态方法</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> SingleTon <span class="title">getInstance</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">if</span> (instance==<span class="keyword">null</span>) {</span><br><span class="line">			<span class="keyword">synchronized</span> (SingleTon<span class="class">.<span class="keyword">class</span>)</span>{</span><br><span class="line">				<span class="keyword">if</span> (instance==<span class="keyword">null</span>) {</span><br><span class="line">					instance = <span class="keyword">new</span> SingleTon();</span><br><span class="line">				}</span><br><span class="line">			}</span><br><span class="line">		}</span><br><span class="line">		<span class="keyword">return</span> instance;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="2-3-7-静态内部类"><a href="#2-3-7-静态内部类" class="headerlink" title="2.3.7 静态内部类"></a>2.3.7 静态内部类</h3><blockquote>
<p><strong>静态内部类的特点</strong></p>
<ul>
<li>当外层的类装载的时候，内部的类不会被装载</li>
<li>调用静态内部类的方法时，静态内部类会被装载，并且只装载一次，线程是安全的</li>
</ul>
</blockquote>
<p><strong>优缺点</strong></p>
<blockquote>
<ul>
<li>采用了类装载机制来保证初始化实例时只有一个线程</li>
<li>静态内部类方式在外部类被装载时并不会立即实例化，而是在需要实例化时，调用方法，才会装载静态内部类，从而完成外部类的实例化</li>
<li>类的静态属性只会在第一次加载类的时候初始化，所以在这里，<code>JVM</code>帮我们保证了线程的安全性，在类进行初始化时，别的线程是无法进入的</li>
<li>避免了<strong>线程不安全</strong>，利用静态内部类特点实现延迟加载，效率高</li>
<li><strong>结论</strong> – 推荐使用</li>
</ul>
</blockquote>
<p><strong>Demo</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">STtest</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		</span><br><span class="line">		SingleTon singleTon1 = SingleTon.getInstance();</span><br><span class="line">		SingleTon singleTon2 = singleTon1.getInstance();</span><br><span class="line">		System.out.println(<span class="string">"singleton1.hashCode: "</span> + singleTon1.hashCode());</span><br><span class="line">		System.out.println(<span class="string">"singleton2.hashCode: "</span> + singleTon2.hashCode());</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SingleTon</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 构造器私有化</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="title">SingleTon</span><span class="params">()</span> </span>{</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">SingleTonInstance</span> </span>{</span><br><span class="line">		<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> SingleTon INSTANCE = <span class="keyword">new</span> SingleTon();</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">synchronized</span> SingleTon <span class="title">getInstance</span><span class="params">()</span> </span>{</span><br><span class="line">		<span class="keyword">return</span> SingleTonInstance.INSTANCE;</span><br><span class="line">	}</span><br><span class="line">	</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="2-3-8-枚举"><a href="#2-3-8-枚举" class="headerlink" title="2.3.8 枚举"></a>2.3.8 枚举</h3><p><strong>优缺点</strong></p>
<blockquote>
<ul>
<li>借助枚举来实现单例模式，不仅能避免多线程同步问题，而且还能防止反序列化重新创建新的对象</li>
<li><strong>结论</strong> – 推荐使用</li>
</ul>
</blockquote>
<p><strong>Demo</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">STtest</span> </span>{</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">		</span><br><span class="line">		SingleTon instance1 = SingleTon.INSTANCE;</span><br><span class="line">		SingleTon instance2 = SingleTon.INSTANCE;</span><br><span class="line">		System.out.println(<span class="string">"instance1.hashCode: "</span> + instance1.hashCode());</span><br><span class="line">		System.out.println(<span class="string">"instance2.hashCode: "</span> + instance2.hashCode());</span><br><span class="line">		instance1.sayOK();</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">enum</span> SingleTon {</span><br><span class="line">	INSTANCE;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sayOK</span><span class="params">()</span> </span>{</span><br><span class="line">		System.out.println(<span class="string">"OK"</span>);</span><br><span class="line">	}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h1 id="三、工厂模式"><a href="#三、工厂模式" class="headerlink" title="三、工厂模式"></a>三、工厂模式</h1><p><strong>引入一个具体的需求</strong></p>
<blockquote>
<ul>
<li>披萨的种类很多，比如<code>GreekPizz</code>、<code>CheesePizz</code></li>
<li>披萨的制作有<code>prepare</code>、<code>bake</code>、<code>cut</code>、<code>box</code></li>
<li>完成披萨店的订购功能</li>
</ul>
</blockquote>
<h2 id="3-1-源码追溯"><a href="#3-1-源码追溯" class="headerlink" title="3.1 - 源码追溯"></a>3.1 - 源码追溯</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Calendar calendar = Calendar.getInstance();</span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Calendar <span class="title">getInstance</span><span class="params">()</span> </span>{</span><br><span class="line">  Locale aLocale = Locale.getDefault(Locale.Category.FORMAT);</span><br><span class="line">  <span class="keyword">return</span> createCalendar(defaultTimeZone(aLocale), aLocale);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> Calendar <span class="title">createCalendar</span><span class="params">(TimeZone zone, Locale aLocale)</span> </span>{</span><br><span class="line">  CalendarProvider provider = LocaleProviderAdapter.getAdapter(CalendarProvider<span class="class">.<span class="keyword">class</span>, <span class="title">aLocale</span>).<span class="title">getCalendarProvider</span>()</span>;</span><br><span class="line">  <span class="keyword">if</span> (provider != <span class="keyword">null</span>) {</span><br><span class="line">    <span class="keyword">try</span> {</span><br><span class="line">      <span class="keyword">return</span> provider.getInstance(zone, aLocale);</span><br><span class="line">    } <span class="keyword">catch</span> (IllegalArgumentException iae) {</span><br><span class="line">    }</span><br><span class="line">  }</span><br><span class="line"></span><br><span class="line">  Calendar cal = <span class="keyword">null</span>;</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">if</span> (aLocale.hasExtensions()) {</span><br><span class="line">    String caltype = aLocale.getUnicodeLocaleType(<span class="string">"ca"</span>);</span><br><span class="line">    <span class="keyword">if</span> (caltype != <span class="keyword">null</span>) {</span><br><span class="line">      <span class="keyword">switch</span> (caltype) {</span><br><span class="line">        <span class="keyword">case</span> <span class="string">"buddhist"</span>:</span><br><span class="line">          cal = <span class="keyword">new</span> BuddhistCalendar(zone, aLocale);</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">"japanese"</span>:</span><br><span class="line">          cal = <span class="keyword">new</span> JapaneseImperialCalendar(zone, aLocale);</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">"gregory"</span>:</span><br><span class="line">          cal = <span class="keyword">new</span> GregorianCalendar(zone, aLocale);</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">      }</span><br><span class="line">    }</span><br><span class="line">  }</span><br><span class="line">  <span class="keyword">if</span> (cal == <span class="keyword">null</span>) {</span><br><span class="line">    <span class="keyword">if</span> (aLocale.getLanguage() == <span class="string">"th"</span> && aLocale.getCountry() == <span class="string">"TH"</span>) {</span><br><span class="line">      cal = <span class="keyword">new</span> BuddhistCalendar(zone, aLocale);</span><br><span class="line">    } <span class="keyword">else</span> <span class="keyword">if</span> (aLocale.getVariant() == <span class="string">"JP"</span> && aLocale.getLanguage() == <span class="string">"ja"</span>&&aLocale.getCountry() == <span class="string">"JP"</span>) {</span><br><span class="line">      cal = <span class="keyword">new</span> JapaneseImperialCalendar(zone, aLocale);</span><br><span class="line">    } <span class="keyword">else</span> {</span><br><span class="line">      cal = <span class="keyword">new</span> GregorianCalendar(zone, aLocale);</span><br><span class="line">    }</span><br><span class="line">  }</span><br><span class="line">  <span class="keyword">return</span> cal;</span><br><span class="line">}</span><br><span class="line"><span class="comment">// 简单工厂模式</span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="3-2-三种模式"><a href="#3-2-三种模式" class="headerlink" title="3.2 - 三种模式"></a>3.2 - 三种模式</h2><h3 id="3-2-1-简单工厂模式"><a href="#3-2-1-简单工厂模式" class="headerlink" title="3.2.1 简单工厂模式"></a>3.2.1 简单工厂模式</h3><blockquote>
<ul>
<li>简单工厂模式是属于<strong>创建型模式</strong>，是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式中最简单实用的模式</li>
<li>定义了一个创建对象的类，由这个类来<strong>封装实例化对象</strong>的行为</li>
<li>在软件开发中，当我们会用到大量的创建某种、某类或者某批对象时，就会用到工厂模式</li>
</ul>
</blockquote>
<p><strong>增加新的需求</strong></p>
<blockquote>
<p>客户在点披萨时，可以点不同口味的披萨，比如北京的奶酪<code>pizza</code>，北京的胡椒<code>pizza</code>或者伦敦的奶酪<code>pizza</code>，伦敦的胡椒<code>pizza</code></p>
</blockquote>
<h3 id="3-2-2-工厂方法模式"><a href="#3-2-2-工厂方法模式" class="headerlink" title="3.2.2 工厂方法模式"></a>3.2.2 工厂方法模式</h3><h3 id="3-2-3-抽象工厂模式"><a href="#3-2-3-抽象工厂模式" class="headerlink" title="3.2.3 抽象工厂模式"></a>3.2.3 抽象工厂模式</h3><blockquote>
<ul>
<li>定义了一个 <code>interface</code> 用于创建相关或有依赖关系的对象簇，而无需指明具体的类</li>
<li>抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合</li>
<li>从设计层面看，抽象工厂模式就是对简单工厂模式的改进</li>
<li>将工厂抽象成两层，抽象工厂和具体实现的工厂子类。程序员可以根据创建对象类型使用对应的工厂子类，这样将简单工厂类变成了工厂簇，更利于代码的维护和扩展</li>
</ul>
</blockquote>
<h2 id="3-3-工厂模式小结"><a href="#3-3-工厂模式小结" class="headerlink" title="3.3 - 工厂模式小结"></a>3.3 - 工厂模式小结</h2><blockquote>
<ul>
<li>工厂模式的意义 – 将实例化对象的代码提取出来，放到一个类中统一管理和为何，达到和主项目的依赖关系的解耦。从而提高项目的扩展和维护性</li>
<li>三种工厂模式</li>
<li>设计模式的<strong>依赖抽象</strong>原则</li>
</ul>
</blockquote>
<blockquote>
<ul>
<li>创建对象实例时，不要直接 new 类，而是把这个 new 类的动作放在一个工厂的方法中，并返回。</li>
<li>不要让类继承具体类，而是继承抽象类或者是实现 <code>interface</code> 接口</li>
<li>不要覆盖基类中已经实现的方法</li>
</ul>
</blockquote>
<h1 id="四、原型模式"><a href="#四、原型模式" class="headerlink" title="四、原型模式"></a>四、原型模式</h1><p><strong>引入一个具体需求</strong></p>
<blockquote>
<p>现有一只羊 <code>tom</code> ，年龄为 <code>1</code>，颜色为 <code>white</code> 请编写程序创建和 <code>tom</code> 属性完全相同的 <code>10</code> 只羊</p>
</blockquote>
<h2 id="4-1-源码追溯"><a href="#4-1-源码追溯" class="headerlink" title="4.1 - 源码追溯"></a>4.1 - 源码追溯</h2><h2 id="4-2-基本介绍"><a href="#4-2-基本介绍" class="headerlink" title="4.2 - 基本介绍"></a>4.2 - 基本介绍</h2><blockquote>
<ul>
<li>用原型实例指定创建对象的种类，并且通过拷贝这些原型，创建新的对象</li>
<li>原型模式是一种<strong>创建型</strong>设计模式，允许一个对象再创建另外一个可定制的对象，无需知道如何创建的细节</li>
<li>工作原理：通过将一个原型对象传给那个要发动创建的对象，这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建，即 <code>对象.clone()</code></li>
</ul>
</blockquote>
<h2 id="4-3-深入讨论"><a href="#4-3-深入讨论" class="headerlink" title="4.3 - 深入讨论"></a>4.3 - 深入讨论</h2><h3 id="4-3-1-浅拷贝"><a href="#4-3-1-浅拷贝" class="headerlink" title="4.3.1 浅拷贝"></a>4.3.1 浅拷贝</h3><blockquote>
<ul>
<li>对于数据类型是基本数据类型的成员变量，浅拷贝会直接进行值传递，也就是将该属性值复制一份给新的对象</li>
<li>对于数据类型是引用数据类型的成员变量，比如是某个数组、某个类的对象等，那么浅拷贝会进行引用传递，也就是只是将该成员变量的引用值（内存地址）复制一份给新的对象。因为实际上两个对象的该成员变量都指向同一个实例。在这种情况下，在一个对象中修改该成员变量会印象另一个对象的该成员变量值</li>
</ul>
</blockquote>
<h3 id="4-3-2-深拷贝"><a href="#4-3-2-深拷贝" class="headerlink" title="4.3.2 深拷贝"></a>4.3.2 深拷贝</h3><blockquote>
<ul>
<li>复制对象的所有基本数据类型的成员变量值</li>
<li>为所有引用数据类型的成员变量申请存储空间，并复制每个引用数据类型成员变量所引用的对象，知道该对象可达的所有对象。也就是说对象进行深拷贝要对整个对象进行拷贝</li>
<li>实现方法<ul>
<li>重写 clone() 方法</li>
<li>通过对象序列化</li>
</ul>
</li>
</ul>
</blockquote>
<h2 id="4-4-注意事项和细节"><a href="#4-4-注意事项和细节" class="headerlink" title="4.4 - 注意事项和细节"></a>4.4 - 注意事项和细节</h2><blockquote>
<ul>
<li>创建新的对象比较复杂时，可以利用原型模式简化对象的创建过程，同时也能提高效率</li>
<li>不用重写初始化对象，而是动态的获得对象运行时的状态</li>
<li>如果原始对象发生变化（增加/减少属性），其他克隆对象也会发生相应的变化，无需修改代码</li>
<li>在实现深克隆的时候可能需要比较复杂的代码</li>
<li><strong>缺点</strong> – 需要为每一个类配备一个克隆方法，这对全新的类来说不是很难，但对已有的类进行改造时，需要修改其原代码，违背了<code>OCP</code>原则</li>
</ul>
</blockquote>
<h1 id="五、建造者模式"><a href="#五、建造者模式" class="headerlink" title="五、建造者模式"></a>五、建造者模式</h1><p><strong>引入一个具体需求</strong></p>
<blockquote>
<p>盖房子需求</p>
<ul>
<li>需要建房子：这一过程为打桩、砌墙、封顶</li>
<li>房子有各种各样的，比如普通房，高楼，别墅，各种房子的过程虽然一样，但是要求不要相同</li>
</ul>
</blockquote>
<h2 id="5-1-基本介绍"><a href="#5-1-基本介绍" class="headerlink" title="5.1 - 基本介绍"></a>5.1 - 基本介绍</h2><blockquote>
<ul>
<li>又叫生成器模式，是一种对象构建模式。它可以将复杂对象的建造过程抽象出来。是这个抽象过程的不同实现方法可以构造出不同表现的对象</li>
<li>是一步一步创建一个复杂的对象，它允许用户只通过指定复杂对象的类型和内容就可以构建它们，用户不需要知道内部的具体构建细节</li>
</ul>
</blockquote>
<h2 id="5-2-建造者模式的四个角色"><a href="#5-2-建造者模式的四个角色" class="headerlink" title="5.2 - 建造者模式的四个角色"></a>5.2 - 建造者模式的四个角色</h2><blockquote>
<ul>
<li><code>Product</code> – 产品角色，一个具体的产品对象</li>
<li><code>Builder</code> – 抽象建造者，创建一个<code>Product</code>对象的各个部件指定的接口</li>
<li><code>ConcreteBuilder</code> – 具体建造者，实现接口，构建和装配各个部件</li>
<li><code>Director</code> – 构建一个使用<code>Builder</code>接口的对象。它主要是用于创建一个复杂的对象。主要有两个作用<ul>
<li>隔离了客户于对象的生产过程</li>
<li>负责控制产品对象的生产过程</li>
</ul>
</li>
</ul>
</blockquote>
<h2 id="5-3-注意事项和细节"><a href="#5-3-注意事项和细节" class="headerlink" title="5.3 - 注意事项和细节"></a>5.3 - 注意事项和细节</h2><blockquote>
<ul>
<li>客户端（使用程序）不必要知道产品内部组成的细节，将产品本身与产品的创建过程解耦，使得相同的创建过程可以创建不同的产品对象</li>
<li>每一个具体建造者都相对独立，而与其他的具体建造者无关，因此可以很方便的替换具体建造者或增加新的具体建造者，用户使用不同的具体建造者即可得到不同的产品对象</li>
<li>可以增加精细地控制产品地创建过程。将复杂产品地创建步骤分解在不同地方法中，使得创建过程更加清晰，也更方便使用程序来控制创建过程</li>
<li>增加新的具体建造者无需修改原有类库地代码，指挥者类针对抽象建造者类变成，系统扩展方便，符合<code>OCP</code></li>
<li>建造者模式所创建的产品一般具有较多的相同点，其组成部分相似，如果产品之间的差异性很大，则不适合使用建造者模式，因此其使用范围收到一定的限制</li>
<li>如果产品的内部变化复杂，可能会导致需要定义很多具体建造者类来实现这种变化，导致系统变得很庞大，因此在这种情况下，要考虑是否选择建造者模式</li>
<li><strong>抽象工厂模式 vs 建造者模式</strong><ul>
<li>抽象工厂模式实现对产品家族的创建，一个产品家族是这样的一系列产品：具有不同分类维度的产品组合，采用抽象工厂模式不需要关心构建过程，只关心产品由什么工厂完成即可</li>
<li>建造者模式则是要求按照指定的蓝图建造产品，它的主要目的是通过组装零件而产生一个新产品</li>
</ul>
</li>
</ul>
</blockquote>
<h1 id="六、适配器模式"><a href="#六、适配器模式" class="headerlink" title="六、适配器模式"></a>六、适配器模式</h1><h2 id="6-1-基本介绍"><a href="#6-1-基本介绍" class="headerlink" title="6.1 - 基本介绍"></a>6.1 - 基本介绍</h2><blockquote>
<ul>
<li>适配器模式将某个类地接口装换成客户端期待地另一个接口表示，主要目的是兼容性，让原本因接口不匹配不能一起工作地两个类可以协同工作，其别名为包装器</li>
<li>适配器属于结构型模式</li>
</ul>
</blockquote>
<h2 id="6-2-工作原理"><a href="#6-2-工作原理" class="headerlink" title="6.2 - 工作原理"></a>6.2 - 工作原理</h2><blockquote>
<ul>
<li>将一个类的接口转换成另一种接口，让原本接口不兼容的类可以兼容</li>
<li>从用户的角度看不见被适配者，是解耦的</li>
<li>用户调用适配器转化出来的目标接口方法，适配器再调用被适配者的相关接口方法</li>
<li>用户接收到返回结果，感觉只是和目标接口交互 </li>
</ul>
</blockquote>
<h2 id="6-3-三种方式"><a href="#6-3-三种方式" class="headerlink" title="6.3 - 三种方式"></a>6.3 - 三种方式</h2><h3 id="6-3-1-类适配器模式"><a href="#6-3-1-类适配器模式" class="headerlink" title="6.3.1 类适配器模式"></a>6.3.1 类适配器模式</h3><blockquote>
<p><strong>模式介绍</strong></p>
<p><code>Adapter</code>类，通过继承<code>src</code>类，实现<code>dst</code>类接口，完成<code>src</code>-><code>dst</code>的适配</p>
</blockquote>
<blockquote>
<p><strong>应用实例</strong></p>
<ul>
<li>以生活中充电器的例子来讲解适配器，充电器本身相当于<code>Adapter</code>，<code>220v</code>交流电相当于<code>src</code>，我们的<code>dst</code>是<code>5v</code>的直流电</li>
</ul>
</blockquote>
<p><strong>注意事项和细节</strong></p>
<blockquote>
<ul>
<li><code>Java</code>是单继承机制，所以类适配器需要继承<code>src</code>类这一点算是一个缺点，因为这要求<code>dst</code>必须是接口，有一定局限性</li>
<li><code>src</code>类的方法在<code>Adapter</code>中会暴露出来，也增加了使用的成本</li>
<li>由于其继承了<code>src</code>类，所以它可以根据需求重写<code>src</code>类的方法，使得<code>Adapter</code>的灵活性增加</li>
</ul>
</blockquote>
<h3 id="6-3-2-对象适配器模式"><a href="#6-3-2-对象适配器模式" class="headerlink" title="6.3.2 对象适配器模式"></a>6.3.2 对象适配器模式</h3><p><strong>基本介绍</strong></p>
<blockquote>
<ul>
<li>基本思路和类的适配器模式相同，，只是将<code>Adapter</code>类作修改，不是继承<code>src</code>类，而是持有<code>src</code>类的实例，以解决兼容性的问题。即：持有<code>src</code>类，实现<code>dst</code>类接口，完成<code>src</code>-><code>dst</code>的适配</li>
<li>根据“合成复用原则”，在系统中尽量使用关联关系来替代继承关系</li>
</ul>
</blockquote>
<p><strong>注意事项和细节</strong></p>
<blockquote>
<ul>
<li>根据合成复用原则，使用组合代替继承，所以它解决了类适配器必须继承<code>src</code>的局限性问题，也并不再要求<code>dst</code>必须是接口</li>
<li>使用成本更低，更灵活</li>
</ul>
</blockquote>
<h3 id="6-3-3-接口适配器模式"><a href="#6-3-3-接口适配器模式" class="headerlink" title="6.3.3 接口适配器模式"></a>6.3.3 接口适配器模式</h3><p><strong>基本介绍</strong></p>
<blockquote>
<ul>
<li>当不需要全部实现接口提供的方法时，可先设计一个抽象类实现接口，并为该接口中的每一个方法提供一个默认的实现方法（<strong>空方法</strong>），那么该抽象类的子类可有选择的覆盖父类的某些来实现需求</li>
<li>适用于一个接口不想使用其所有方法的情况</li>
</ul>
</blockquote>
<h2 id="6-4-注意事项和细节"><a href="#6-4-注意事项和细节" class="headerlink" title="6.4 - 注意事项和细节"></a>6.4 - 注意事项和细节</h2><blockquote>
<ul>
<li>类适配器 – 以类给到，在<code>Adapter</code>里，就是将<code>src</code>当作类继承</li>
<li>对象适配器 – 以对象给到，在<code>Adapter</code>里，将<code>src</code>作为一个对象持有</li>
<li>接口适配器 – 以接口给到，在<code>Adapter</code>里，将<code>src</code>作为一个接口实现</li>
<li><code>Adapter</code>模式最大的作用还是将原本不兼容的接口融合在一起工作</li>
<li>实际开发中，实现起来不拘泥于我们讲解的三种经典模式</li>
</ul>
</blockquote>
<h1 id="七、桥接模式"><a href="#七、桥接模式" class="headerlink" title="七、桥接模式"></a>七、桥接模式</h1><h2 id="7-1-基本介绍"><a href="#7-1-基本介绍" class="headerlink" title="7.1 - 基本介绍"></a><strong>7.1 - 基本介绍</strong></h2><blockquote>
<ul>
<li>将实现与抽象放在两个不同的类层次中，使两个层次可以独立改变</li>
<li>是一种结构型设计模式</li>
<li>基于类的最小设计模式，通过封装、聚合、继承等行为让不同的类承担不同的职责，主要特点使把抽象与行为实现分离开来，从而可以保持各部分的独立性以及应对它们的功能扩展</li>
</ul>
</blockquote>
<h2 id="7-2-注意事项及细节"><a href="#7-2-注意事项及细节" class="headerlink" title="7.2 - 注意事项及细节"></a><strong>7.2 - 注意事项及细节</strong></h2><blockquote>
<ul>
<li>实现了抽象和实现部分的分离，从而极大的提供了系统的灵活性，让抽象部分和是心啊部分独立开来，者有助于系统进行分层设计，从而产生更好的结构化系统</li>
<li>对于系统的高层部分，只要知道抽象部分和实现部分的接口就可以了，其他部分由具体业务来完成</li>
<li>桥接模式替代多层继承方案，可以减少子类的个数，降低系统的管理和维护成本</li>
<li>桥接模式的引入增加了系统的理解和设计难度，由于聚合关联关系建立在抽象层，要求开发者针对抽象进行设计和编程</li>
<li>桥接模式要求正确识别出系统中两个独立变化的维度，因此其使用具体范围有一定的局限性，即需要有这样的应用场景</li>
</ul>
</blockquote>
<h1 id="八、装饰者模式"><a href="#八、装饰者模式" class="headerlink" title="八、装饰者模式"></a>八、装饰者模式</h1><h2 id="8-1-基本介绍"><a href="#8-1-基本介绍" class="headerlink" title="8.1 - 基本介绍"></a><strong>8.1 - 基本介绍</strong></h2><blockquote>
<ul>
<li><strong>动态的</strong>将新功能附加到对象上。在对象功能扩展方面，它比继承更有弹性，装饰者模式也体现了开闭原则<code>OCP</code></li>
</ul>
</blockquote>
<h1 id="九、组合模式"><a href="#九、组合模式" class="headerlink" title="九、组合模式"></a>九、组合模式</h1><h2 id="9-1-基本介绍"><a href="#9-1-基本介绍" class="headerlink" title="9.1 - 基本介绍"></a><strong>9.1 - 基本介绍</strong></h2><blockquote>
<ul>
<li>又叫部分整体模式，它创建了对象组的树形结构，将对象组合成树状结构以表示<strong>整体-部分</strong>的层次关系</li>
<li>组合模式依据树形结构来组合对象，用来表示部分以及整体层次</li>
<li>这种类型的设计模式属于结构型模式</li>
<li>组合模式使得用户对单个对象和组合对象的访问具有一致性，即组合能让客户以一致的方式处理个别对象以及组合对象</li>
</ul>
</blockquote>
<h2 id="9-2-注意事项及细节"><a href="#9-2-注意事项及细节" class="headerlink" title="9.2 - 注意事项及细节"></a><strong>9.2 - 注意事项及细节</strong></h2><blockquote>
<ul>
<li>简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题</li>
<li>具有较强的扩展性。当我们要更改组合对象时，我们只需要调正内部的层次关系，客户端不需要做出任何改动</li>
<li>方便创建出复杂的层次结构。客户端不用理会组合里的组成细节，容易添加节点或者叶子从而创建出复杂的树形结构</li>
<li>需要遍历组织机构，或者处理的对象具有树形结构时，非常适合使用组合模式</li>
<li>要求较高的抽象性，如果节点和叶子节点有很多差异性的话，比如很多方法和属性都不一样，不适合使用组合模式</li>
</ul>
</blockquote>
<h1 id="十、外观模式"><a href="#十、外观模式" class="headerlink" title="十、外观模式"></a>十、外观模式</h1><h2 id="10-1-基本介绍"><a href="#10-1-基本介绍" class="headerlink" title="10.1 - 基本介绍"></a><strong>10.1 - 基本介绍</strong></h2><blockquote>
<ul>
<li>也叫过程模式，外观模式为子系统中的一组接口提供一个一致的界面，此模式定义了一个高层接口，这个接口使得这一子系统更加容易使用</li>
<li>外观模式通过定义一个一致的接口，用以屏蔽内部子系统的细节，使得调用端口只需跟这个接口发生调用，而无需关心这个子系统的内部细节</li>
</ul>
</blockquote>
<h2 id="10-2-注意事项和细节"><a href="#10-2-注意事项和细节" class="headerlink" title="10.2 - 注意事项和细节"></a><strong>10.2 - 注意事项和细节</strong></h2><blockquote>
<ul>
<li>对外屏蔽了子系统的细节，因此外观模式降低了客户端对子系统使用的复杂性</li>
<li>外观模式对客户端与子系统的耦合关系，让子系统内部的模块更容易维护和扩展</li>
<li>通过合理的使用外观模式，可以帮我们更好的划分访问层次</li>
<li>当系统需要进行分层设计师，可以考虑使用外观模式</li>
<li>在维护一个遗留的大型系统时，可能这个系统已经变得非常难以维护和发展，此时可以考虑为新的系统开发一个外观模式类，来提供遗留系统的比较清晰简单的接口，让新的系统与外观模式类交互，提高复用性</li>
<li>不能过多的或者不合理的使用外观模式，使用外观模式号，还是直接调用模块号。要以让系统有层次，利于维护为目的</li>
</ul>
</blockquote>
<h1 id="十一、享元模式"><a href="#十一、享元模式" class="headerlink" title="十一、享元模式"></a>十一、享元模式</h1><h2 id="11-1-基本介绍"><a href="#11-1-基本介绍" class="headerlink" title="11.1 - 基本介绍"></a><strong>11.1 - 基本介绍</strong></h2><blockquote>
<ul>
<li>又叫蝇量模式，运用共享技术有效的支持大量细粒度的对象</li>
<li>常用于系统底层开发，解决系统的性能问题。像数据库连接池，里面都是创建好的连接对象，这这些连接对象中有我们需要的则直接拿来用，避免重新创建，如果没有我们需要的，则创建一个</li>
<li>享元模式能够解决重复对象的内存浪费问题，当系统中有大量相似对象，需要缓冲池时，不需要总是创建新对象，可以从缓冲池里拿。这样可以降低系统内存，同时提高效率</li>
</ul>
</blockquote>
<h2 id="11-2-内部状态和外部状态"><a href="#11-2-内部状态和外部状态" class="headerlink" title="11.2 - 内部状态和外部状态"></a><strong>11.2 - 内部状态和外部状态</strong></h2><blockquote>
<ul>
<li>享元模式提出了两个要求：细粒度和共享对象，这里就涉及到内部状态和外部状态了，即对象的信息分为两个部分</li>
<li><strong>内部状态</strong> – 即对象共享出来的信息，存储在享元对象内部且不会随环境的改变而改变</li>
<li><strong>外部状态</strong> – 即对象得以依赖的一个标记，是随环境改变而改变的、不可共享的状态</li>
</ul>
</blockquote>
<h2 id="11-3-注意事项及细节"><a href="#11-3-注意事项及细节" class="headerlink" title="11.3 - 注意事项及细节"></a><strong>11.3 - 注意事项及细节</strong></h2><blockquote>
<ul>
<li>享 - “共享”，元 - “对象”</li>
<li>系统中有大量对象，这些对象消耗大量内存，并且对象的状态大部分可以外部化时，我们就可以考虑选用享元模式</li>
<li>用唯一识别码判断，如果在内存中有，则返回这个唯一标识码所标识的对象，用<code>HashMap</code>/<code>HashTable</code>存储</li>
<li>享元模式大大减少了对象的创建，降低了程序内存的占用，提高效率</li>
<li>享元模式提高了系统的复杂度。需要分理处内部状态和外部状态，而外部状态具有固化特性，不应该随着内部状态的改变而改变，这是我们使用享元模式需要注意的地方</li>
<li>使用享元模式时，注意划分内部状态和外部状态，并且需要有一个工厂类加以控制</li>
<li>享元模式经典的应用场景是需要缓冲池的场景</li>
</ul>
</blockquote>
<h1 id="十二、代理模式"><a href="#十二、代理模式" class="headerlink" title="十二、代理模式"></a>十二、代理模式</h1><h2 id="12-1-基本介绍"><a href="#12-1-基本介绍" class="headerlink" title="12.1 - 基本介绍"></a>12.1 - 基本介绍</h2><blockquote>
<ul>
<li>代理模式：作为一个对象提供一个替身，以控制对这个对象的访问。即通过代理对象访问目标对象。这样做的好处是：可以在目标对象事项的基础上，增强额外的功能操作，即扩展目标对象的功能</li>
<li>被代理的对象可以是远程对象、创建开销大的对象或需要安全控制的对象</li>
<li>代理模式有不同的形式、主要有三种<ul>
<li><strong>静态代理</strong> – 基于接口</li>
<li><strong>动态代理</strong> – 基于接口，也称<code>JDK代理</code>或<code>接口代理</code></li>
<li><strong>Cglib</strong>代理 – 可以在内存动态创建对象，而不需要实现接口</li>
</ul>
</li>
</ul>
</blockquote>
<h2 id="12-2-几种模式"><a href="#12-2-几种模式" class="headerlink" title="12.2 - 几种模式"></a>12.2 - 几种模式</h2><h3 id="12-2-1-静态代理"><a href="#12-2-1-静态代理" class="headerlink" title="12.2.1 静态代理"></a>12.2.1 静态代理</h3><blockquote>
<p>静态代理在使用时，需要定义接口或者父类，被代理对象（即目标对象）与代理对象一起实现相同的接口或者是继承相同父类</p>
</blockquote>
<blockquote>
<ul>
<li>优点：在不修改目标对象的功能前提下，能通过代理对象对目标功能扩展</li>
<li>缺点：因为代理对象需要与目标对象实现一样的接口，所以会有很多代理对象嘞</li>
<li>一旦接口增加方法，目标对象和代理对象都要维护</li>
</ul>
</blockquote>
<h3 id="12-2-2-动态代理"><a href="#12-2-2-动态代理" class="headerlink" title="12.2.2 动态代理"></a>12.2.2 动态代理</h3><blockquote>
<ul>
<li>代理对象不需要实现接口，代理目标对象要实现接口，否则不能动态代理</li>
<li>代理对象的生成时利用<code>JDK</code>的<code>API</code>，动态的在内存中构建代理对象</li>
<li>动态代理也叫做：<code>JDK代理</code>、<code>接口代理</code></li>
</ul>
</blockquote>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">static</span> Object <span class="title">newProxyInstance</span><span class="params">(ClassLoader loader, Class<!--?-->[] interfaces, InvocationHandler h)</span></span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"> <span class="comment">/*</span></span></span><br><span class="line"><span class="function"><span class="comment">  * ClassLoader loader：指定当前目标对象使用的类加载器，获取加载器的方法固定</span></span></span><br><span class="line"><span class="function"><span class="comment">  * Class<!--?-->[] interface：目标对象实现的接口类型，使用泛型发给发确认类型</span></span></span><br><span class="line"><span class="function"><span class="comment">  * InvocationHandler h：事情处理，执行目标对象的方法时，会出发事情处理器方法</span></span></span><br><span class="line"><span class="function"><span class="comment">  */</span></span></span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="12-2-3-Cglib代理"><a href="#12-2-3-Cglib代理" class="headerlink" title="12.2.3 Cglib代理"></a>12.2.3 Cglib代理</h3><blockquote>
<ul>
<li>静态代理和<code>JDK</code>代理模式都要求目标对象是实现一个接口，但是有时候目标对象只是一个单独的对象，并没有实现任何的接口，这个时候可使用目标对象子类来实现代理</li>
<li><code>Cglib代理</code>也叫做子类代理，它是在内存中构建一个子类对象，从而实现对目标对象功能扩展，</li>
<li><code>Cglib</code>是一个强大的高性能的代码生成包，它可以再运行期扩展<code>Java</code>类与实现<code>Java</code>接口，它广泛的被许多<code>AOP</code>的框架使用，例如<code>Spring AOP</code>，实现方法蓝杰</li>
<li>在<code>AOP</code>编程中如何选择代理模式：<ul>
<li>目标对象需要实现接口，用<code>JDK</code>代理</li>
<li>目标对象不需要实现接口，用<code>Cglib</code>代理</li>
</ul>
</li>
<li><code>Cglib包</code>的底层是通过使用字节码处理框架<code>ASM</code>来转换字节码并生成新的类</li>
</ul>
</blockquote>
<p><strong>实现步骤</strong></p>
<blockquote>
<ul>
<li>需要引入<code>Cglib</code>的<code>jar</code>包</li>
<li>在内存中动态构建子类，注意代理的类不能为<code>final</code>，否则报错<code>java.lang.illegalArgumentException</code></li>
<li>目标对象的方法如果为<code>final/static</code>，那么就不会被拦截，即不会执行目标对象额外的业务方法</li>
</ul>
</blockquote>
<h1 id="十三、模板方法模式"><a href="#十三、模板方法模式" class="headerlink" title="十三、模板方法模式"></a>十三、模板方法模式</h1><h2 id="13-1-基本介绍"><a href="#13-1-基本介绍" class="headerlink" title="13.1 - 基本介绍"></a>13.1 - 基本介绍</h2><blockquote>
<ul>
<li>又交模板模式，在一个抽象类公开定义了执行它的方法的模板，它的子类可以按需要重写方法实现，但调用将以抽象类中定义的方式进行</li>
<li>简单说，<strong>模板方法模式</strong>定义了一个操作中算法的股价，而将一些步骤延迟到子类中，使得子类可以不改变一个算法的结构，就可以重定义该算法的某些特定步骤</li>
<li>属于行为型模式</li>
</ul>
</blockquote>
<h2 id="13-2-钩子方法"><a href="#13-2-钩子方法" class="headerlink" title="13.2 - 钩子方法"></a>13.2 - 钩子方法</h2><blockquote>
<ul>
<li>在模板方法模式的父类中，我们可以定义一个方法，它默认不做任何事，子类可以事情框框要不要覆盖他，该方法成为“钩子”</li>
</ul>
</blockquote>
<h1 id="十四、命令模式"><a href="#十四、命令模式" class="headerlink" title="十四、命令模式"></a>十四、命令模式</h1><h2 id="14-1-基本介绍"><a href="#14-1-基本介绍" class="headerlink" title="14.1 - 基本介绍"></a>14.1 - 基本介绍</h2><blockquote>
<ul>
<li>在软件设计中，我们经常需要向某些对象发送请求，但是并不知道请求的接收者是谁，也不知道被请求的操作是哪个，我们只需在程序运行时指定具体的请求接收者即可，此时，可以使用我们的命令模式来进行设计</li>
<li>命名模式使得请求发送者与请求接收者消除彼此之间的耦合，让对象之间的调用关系更加灵活，实现解耦</li>
<li>在命令模式中，会将一个请求封装为一个对象，一边使用不同参数来标识不同的请求，同时命令模式也支持可撤销的操作<ul>
<li><code>Invoker</code>是调用者</li>
<li><code>Receiver</code>是被调用者</li>
<li><code>MyCommand</code>是命令</li>
</ul>
</li>
</ul>
</blockquote>
<h2 id="14-2-注意事项及细节"><a href="#14-2-注意事项及细节" class="headerlink" title="14.2 - 注意事项及细节"></a>14.2 - 注意事项及细节</h2><blockquote>
<ul>
<li>将发起请求的对象与执行请求的对象解耦。发起请求的对象是调用者，调用者只要调用命令对象的<code>execute()</code>方法就可以让接收者工作，而不必知道具体的接收者对象是谁、如何实现的，命令对象会负责让接收者执行请求的动作，也就是说：“请求发起者”和“请求执行者”之间的解耦是通过命令对象实现的，命令对象起到了纽带桥梁的作用</li>
<li>容易设计一个命令队列，只要把命令对象放到队列，就可以多线程的执行命令</li>
<li>容易实现对请求的撤销和重做</li>
<li>命令模式不足：可能导致某些系统有过多的具体命令类，增加了系统的复杂度，这点在使用的时候要注意</li>
<li>空命令也是一种设计模式，它为我们省去了判空的操作</li>
<li>命令模式经典的应用场景：界面的一个按钮都是一条命令、模拟<code>CMD</code>订单的撤销/恢复、触发-反馈机制</li>
</ul>
</blockquote>
<h1 id="十五、访问者模式"><a href="#十五、访问者模式" class="headerlink" title="十五、访问者模式"></a>十五、访问者模式</h1><h2 id="15-1-基本介绍"><a href="#15-1-基本介绍" class="headerlink" title="15.1 - 基本介绍"></a>15.1 - 基本介绍</h2><blockquote>
<ul>
<li>访问者模式，封装一些作用于某种数据结构的各元素的操作，它可以在不改变数据结构的前提下定义作用于这些元素的新的操作</li>
<li>主要将数据结构与数据操作分离，解决<strong>数据结构</strong>和操作<strong>耦合性</strong>问题</li>
<li><strong>访问者模式的基本工作原理是</strong>：在被访问的类里面加一个对外提供接待访问者的接口</li>
<li><strong>访问者模式的主要应用场景</strong>： 需要对一个对象结构中的对象进行很多不同操作，这些操作彼此没有关联，同时需要避免让这些操作“污染”这些对象的类，可以选用访问者模式解决</li>
</ul>
</blockquote>
<h2 id="15-2-注意事项及细节"><a href="#15-2-注意事项及细节" class="headerlink" title="15.2 - 注意事项及细节"></a>15.2 - 注意事项及细节</h2><blockquote>
<p><strong>优点</strong></p>
<ul>
<li>访问者模式符合单一职责原则，让程序具有优秀的扩展性、灵活性高</li>
<li>访问者模式可以对功能进行统一，可以用来做报表、UI、拦截器和过滤器，适用于数据结构相对稳定的系统</li>
</ul>
<p><strong>缺点</strong></p>
<ul>
<li>具体元素对访问者公布细节，也就是说访问者关注了其他类的内部细节，这是迪米特原则所不建议的，这样造成了具体元素变更比较困难</li>
<li>违背依赖倒转原则。访问者依赖的是具体元素，而不是抽象元素</li>
<li>如果一个系统有比较稳定的数据结构，又有经常变化的功能需求，那么访问者模式就是比较合适的</li>
</ul>
</blockquote>
<h1 id="十六、迭代器模式"><a href="#十六、迭代器模式" class="headerlink" title="十六、迭代器模式"></a>十六、迭代器模式</h1><h2 id="16-1-基本介绍"><a href="#16-1-基本介绍" class="headerlink" title="16.1 - 基本介绍"></a>16.1 - 基本介绍</h2><blockquote>
<ul>
<li>是常用的设计模式，属于行为模式</li>
<li>如果我们的集合元素是用不同方式实现的，有数组，还有<code>Java</code>的集合类，或者还有其他方式，当客户端要遍历这些集合元素的时候就要使用多种遍历方式，而且还会暴露元素的内部结构，可以考虑使用迭代器模式解决</li>
<li>迭代器模式，提供一种遍历集合的统一接口，用一致的方法遍历集合元素，不需要知道集合对象的底层标识</li>
</ul>
</blockquote>
<h2 id="16-2-注意事项及细节"><a href="#16-2-注意事项及细节" class="headerlink" title="16.2 - 注意事项及细节"></a>16.2 - 注意事项及细节</h2><blockquote>
<p><strong>优点</strong></p>
<ul>
<li>提供一个统一的方法遍历对象，客户不用再考虑聚合的类型，使用一种方法就可以遍历对象了。</li>
<li>隐藏了聚合的内部结构，客户端要遍历聚合的时候只能取到迭代器，而不会知道聚合的内部具体组成</li>
<li>提供了一种设计思想，就是一个类应该只有一个引起变化的原因，在聚合类中，我们把迭代器分开，就是要把管理对象集合和遍历对象集合的责任分开，这样一来集合改变的话，只影响到聚合对象。而如果遍历方式改变的话，只影响了迭代器</li>
<li>当要展示一组相似对象，或者遍历一组相同对象时使用，适合使用迭代器模式</li>
</ul>
<p><strong>缺点</strong></p>
<ul>
<li>每个聚合对象都要一个迭代器，会生成多个迭代器不好管理类</li>
</ul>
</blockquote>
<h1 id="十七、观察者模式"><a href="#十七、观察者模式" class="headerlink" title="十七、观察者模式"></a>十七、观察者模式</h1><h2 id="17-1-原理"><a href="#17-1-原理" class="headerlink" title="17.1 - 原理"></a>17.1 - 原理</h2><blockquote>
<ul>
<li>注册 – <code>register/add</code></li>
<li>移除 – <code>remove/delete</code></li>
<li>通知 – <code>notify</code></li>
</ul>
</blockquote>
<h1 id="十八、中介者模式"><a href="#十八、中介者模式" class="headerlink" title="十八、中介者模式"></a>十八、中介者模式</h1><h2 id="18-1-基本介绍"><a href="#18-1-基本介绍" class="headerlink" title="18.1 - 基本介绍"></a>18.1 - 基本介绍</h2><blockquote>
<ul>
<li>用一个中介对象来封装一系列对象交互，中介者使各个对象不需要显式的互相引用，从而使其耦合松散，而且可以独立的改变他们之间的交互</li>
<li>中介者模式术语行为型模式，使代码易于维护</li>
</ul>
</blockquote>
<h2 id="18-2-注意事项及细节"><a href="#18-2-注意事项及细节" class="headerlink" title="18.2 - 注意事项及细节"></a>18.2 - 注意事项及细节</h2><blockquote>
<ul>
<li>多个类相互耦合，会形成网状结构，使用中介者模式将网状结构分离为星型结构，进行解耦</li>
<li>减少类间依赖，降低了耦合，符合迪米特法则</li>
<li>中介者承担了较多的责任，一旦中介者出现问题，整个系统就会受到影响</li>
<li>如果设计不当，中介者对象本身变得过于复杂</li>
</ul>
</blockquote>
<h1 id="十九、备忘录模式"><a href="#十九、备忘录模式" class="headerlink" title="十九、备忘录模式"></a>十九、备忘录模式</h1><h2 id="19-1-基本介绍"><a href="#19-1-基本介绍" class="headerlink" title="19.1 - 基本介绍"></a>19.1 - 基本介绍</h2><blockquote>
<ul>
<li>备忘录模式，在不破坏封装性的前提下，捕获一个对象的内部状态，并在该对象之外保存这个状态，这样以后就可以将该对象恢复到原先保存的状态</li>
<li>属于行为型模式</li>
</ul>
</blockquote>
<h2 id="19-2-注意事项及细节"><a href="#19-2-注意事项及细节" class="headerlink" title="19.2 - 注意事项及细节"></a>19.2 - 注意事项及细节</h2><blockquote>
<ul>
<li>给用户提供了一种可以恢复状态的机制，可以使用户能够比较方便的回到某个历史的状态</li>
<li>实现了信息的封装，使得用户不需要关心状态的保存细节</li>
<li>如果类的成员变量过多，势必会占用较大的资源，而且每一次保存都会消耗一定的内存</li>
<li>为了节约内存，备忘录模式可以和原型模式配合使用</li>
</ul>
</blockquote>
<h1 id="二十、解释器模式"><a href="#二十、解释器模式" class="headerlink" title="二十、解释器模式"></a>二十、解释器模式</h1><h2 id="20-1-基本介绍"><a href="#20-1-基本介绍" class="headerlink" title="20.1 - 基本介绍"></a>20.1 - 基本介绍</h2><blockquote>
<ul>
<li>在编译原理中，一个算术表达式通过词法分析器形成词法单元，而后这些词法单元再通过语法分析器构建语法分析树，最终形成一颗抽象的语法分析树。这里的词法分析器和语法分析器都可以看作是解析器</li>
<li>是给定一个语言，定义它的语法的一种表示，并定义一个解释器，使用该解释器来解释语言中的句子</li>
</ul>
</blockquote>
<h2 id="20-2-注意事项及细节"><a href="#20-2-注意事项及细节" class="headerlink" title="20.2 - 注意事项及细节"></a>20.2 - 注意事项及细节</h2><blockquote>
<ul>
<li>当有一个语言需要解释执行，可将该语言中的句子表示为一个抽象语法树，就可以考虑使用解释器模式，让程序具有良好的扩展性</li>
<li>使用解释器可能带来的问题：解释器模式会引起类膨胀、解释器模式采用递归调用方法，将会导致调试非常复杂、效率可能减低</li>
</ul>
</blockquote>
<h1 id="二十一、状态模式"><a href="#二十一、状态模式" class="headerlink" title="二十一、状态模式"></a>二十一、状态模式</h1><h2 id="21-1-基本介绍"><a href="#21-1-基本介绍" class="headerlink" title="21.1 - 基本介绍"></a>21.1 - 基本介绍</h2><blockquote>
<ul>
<li>主要是用来解决对象在多种状态转换时，需要对外输出不同的行为的问题，状态和行为是一一对应的，状态之间可以互相转换</li>
<li>当一个对象的内在状态改变时，允许改变其行为，这个对象看起来像是改变了其类</li>
</ul>
</blockquote>
<h2 id="21-2-注意事项及细节"><a href="#21-2-注意事项及细节" class="headerlink" title="21.2 - 注意事项及细节"></a>21.2 - 注意事项及细节</h2><blockquote>
<ul>
<li>代码有很强的可读性。状态模式将每个状态的行为封装到对应的一个类中</li>
<li>方便维护。将容易产生问题的 <code>if-else</code> 语句删除了，如果把每个状态的行为都放到一个类中，每次调用方法时都要判断当前是什么状态，不但会产出很多 <code>if-else</code> 语句，而且容易出错</li>
<li>符合 <code>OCP</code>，容易增删状态</li>
<li>会产生很多类，每个状体都要一个对应的类，当状态过多时会产生很多类，加大维护难度</li>
<li>当一个事件或者对象有很多种状态，状态之间会相互转换，对不同的状态要求不同的行为的时候，可以考虑使用状态模式</li>
</ul>
</blockquote>
<h1 id="二十二、策略模式"><a href="#二十二、策略模式" class="headerlink" title="二十二、策略模式"></a>二十二、策略模式</h1><h2 id="22-1-基本介绍"><a href="#22-1-基本介绍" class="headerlink" title="22.1 - 基本介绍"></a>22.1 - 基本介绍</h2><blockquote>
<ul>
<li>定义算法族，分别封装起来，让他们之间可以互相替换，此模式让算法的变化独立于使用算法的用户</li>
<li>这算法体现了几个设计原则<ul>
<li>把变化的代码从不变的代码中分离出来</li>
<li>针对接口编程而不是具体类</li>
<li>多用组合/聚合，少用继承</li>
</ul>
</li>
</ul>
</blockquote>
<h2 id="22-2-注意事项及细节"><a href="#22-2-注意事项及细节" class="headerlink" title="22.2 - 注意事项及细节"></a>22.2 - 注意事项及细节</h2><blockquote>
<ul>
<li>关键是分析项目中变化部分与不变部分</li>
<li>策略模式的核心思想是多用组合/聚合，少用继承，用行为类组合，而不是行为的继承，更有弹性</li>
<li>体现了 <code>OCP</code> 原则，客户端增加行为不用修改原有代码，只要添加一种策略即可，避免了使用多重转移语句</li>
<li>提供了可以替换继承关系的办法，策略模式将算法封装在独立的类，使得可以独立于另一个类改变他，使他易于切换、理解、扩展</li>
<li>需要注意的是，每添加一个策略就会增加一个类，当策略过多会导致类数目庞大</li>
</ul>
</blockquote>
<h1 id="二十三、职责链模式"><a href="#二十三、职责链模式" class="headerlink" title="二十三、职责链模式"></a>二十三、职责链模式</h1><h2 id="23-1-基本介绍"><a href="#23-1-基本介绍" class="headerlink" title="23.1 - 基本介绍"></a>23.1 - 基本介绍</h2><blockquote>
<ul>
<li>为请求创建了一个接收者对象的链。这种模式对请求的发送者和接收者进行解耦</li>
<li>职责链模式通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求，那么他会把相同的请求传给下一个接收者，依此类推</li>
<li>属于行为型模式</li>
</ul>
</blockquote>
<h2 id="23-2-注意事项及细节"><a href="#23-2-注意事项及细节" class="headerlink" title="23.2 - 注意事项及细节"></a>23.2 - 注意事项及细节</h2><blockquote>
<ul>
<li>将请求和处理分开，实现解耦，提高系统的灵活性</li>
<li>简化了对象，使对象不需要知道链的结构</li>
<li>性能会受到影响，特别是链比较长的时候，因此需控制链中最大节点数量</li>
<li>调试不方便，采用了类似递归的方式，调试时逻辑可能比较复杂</li>
</ul>
</blockquote>
</body></html></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">Cying-承影</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://cyingk.github.io/2020/01/16/Design_Pattern/">http://cyingk.github.io/2020/01/16/Design_Pattern/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Java/">Java</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/01/27/Spring/"><img class="prev-cover" src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2516020377,3847466993&amp;fm=11&amp;gp=0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">Spring</div></div></a></div><div class="next-post pull-right"><a href="/2019/12/21/Network/"><img class="next-cover" src="http://img.taopic.com/uploads/allimg/1305" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">Network</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2020/02/26/UML/" title="UML"><img class="cover" src="https://upload-images.jianshu.io/upload_images/5336514-985132f6977c5d33.png?imageMogr2/auto-orient/strip|imageView2/2/w/694/format/webp" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-26</div><div class="title">UML</div></div></a></div><div><a href="/2019/12/13/SQL_To_Java/" title="JDBC"><img class="cover" src="https://img-blog.csdnimg.cn/20191213203223906.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-12-13</div><div class="title">JDBC</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">1.</span> <span class="toc-text">Java 设计模式</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%9F%BA%E7%A1%80"><span class="toc-number">2.</span> <span class="toc-text">一、基础</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E7%9A%84%E9%87%8D%E8%A6%81%E6%80%A7"><span class="toc-number">2.1.</span> <span class="toc-text">1.1 - 设计模式的重要性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E7%9A%84%E7%9B%AE%E7%9A%84"><span class="toc-number">2.2.</span> <span class="toc-text">1.2 - 设计模式的目的</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E7%9A%84%E4%B8%83%E5%A4%A7%E5%8E%9F%E5%88%99"><span class="toc-number">2.3.</span> <span class="toc-text">1.3 - 设计模式的七大原则</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-1-%E5%8D%95%E4%B8%80%E8%81%8C%E8%B4%A3%E5%8E%9F%E5%88%99"><span class="toc-number">2.3.1.</span> <span class="toc-text">1.3.1 单一职责原则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-2-%E6%8E%A5%E5%8F%A3%E9%9A%94%E7%A6%BB%E5%8E%9F%E5%88%99"><span class="toc-number">2.3.2.</span> <span class="toc-text">1.3.2 接口隔离原则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-3-%E4%BE%9D%E8%B5%96%E5%80%92%E8%BD%AC%E5%8E%9F%E5%88%99"><span class="toc-number">2.3.3.</span> <span class="toc-text">1.3.3 依赖倒转原则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-4-%E9%87%8C%E6%B0%8F%E6%9B%BF%E6%8D%A2%E5%8E%9F%E5%88%99"><span class="toc-number">2.3.4.</span> <span class="toc-text">1.3.4 里氏替换原则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-5-%E5%BC%80%E9%97%AD%E5%8E%9F%E5%88%99"><span class="toc-number">2.3.5.</span> <span class="toc-text">1.3.5 开闭原则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-6-%E8%BF%AA%E7%B1%B3%E7%89%B9%E5%8E%9F%E5%88%99"><span class="toc-number">2.3.6.</span> <span class="toc-text">1.3.6 迪米特原则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-7-%E5%90%88%E6%88%90%E5%A4%8D%E7%94%A8%E5%8E%9F%E5%88%99"><span class="toc-number">2.3.7.</span> <span class="toc-text">1.3.7 合成复用原则</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-4-UML-%E7%B1%BB%E5%9B%BE"><span class="toc-number">2.4.</span> <span class="toc-text">1.4 - UML 类图</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F"><span class="toc-number">3.</span> <span class="toc-text">二、单例模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E6%BA%90%E7%A0%81%E8%BF%BD%E6%BA%AF"><span class="toc-number">3.1.</span> <span class="toc-text">2.1 - 源码追溯</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%92%8C%E7%BB%86%E8%8A%82%E8%AF%B4%E6%98%8E"><span class="toc-number">3.2.</span> <span class="toc-text">2.2 - 注意事项和细节说明</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E5%85%AB%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="toc-number">3.3.</span> <span class="toc-text">2.3 - 八种方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-1-%E9%A5%BF%E6%B1%89%E5%BC%8F-%E9%9D%99%E6%80%81%E5%B8%B8%E9%87%8F"><span class="toc-number">3.3.1.</span> <span class="toc-text">2.3.1 饿汉式 - 静态常量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-2-%E9%A5%BF%E6%B1%89%E5%BC%8F-%E9%9D%99%E6%80%81%E4%BB%A3%E7%A0%81%E5%9D%97"><span class="toc-number">3.3.2.</span> <span class="toc-text">2.3.2 饿汉式 - 静态代码块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-3-%E6%87%92%E6%B1%89%E5%BC%8F-%E2%80%93-%E7%BA%BF%E7%A8%8B%E4%B8%8D%E5%AE%89%E5%85%A8"><span class="toc-number">3.3.3.</span> <span class="toc-text">2.3.3 懒汉式 – 线程不安全</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-4-%E6%87%92%E6%B1%89%E5%BC%8F-%E2%80%93-%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%EF%BC%8C%E5%90%8C%E6%AD%A5%E6%96%B9%E6%B3%95"><span class="toc-number">3.3.4.</span> <span class="toc-text">2.3.4 懒汉式 – 线程安全，同步方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-5-%E6%87%92%E6%B1%89%E5%BC%8F-%E2%80%93-%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%EF%BC%8C%E5%90%8C%E6%AD%A5%E4%BB%A3%E7%A0%81%E5%9D%97"><span class="toc-number">3.3.5.</span> <span class="toc-text">2.3.5 懒汉式 – 线程安全，同步代码块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-6-%E5%8F%8C%E9%87%8D%E6%A3%80%E6%9F%A5"><span class="toc-number">3.3.6.</span> <span class="toc-text">2.3.6 双重检查</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-7-%E9%9D%99%E6%80%81%E5%86%85%E9%83%A8%E7%B1%BB"><span class="toc-number">3.3.7.</span> <span class="toc-text">2.3.7 静态内部类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-8-%E6%9E%9A%E4%B8%BE"><span class="toc-number">3.3.8.</span> <span class="toc-text">2.3.8 枚举</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.</span> <span class="toc-text">三、工厂模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E6%BA%90%E7%A0%81%E8%BF%BD%E6%BA%AF"><span class="toc-number">4.1.</span> <span class="toc-text">3.1 - 源码追溯</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E4%B8%89%E7%A7%8D%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.2.</span> <span class="toc-text">3.2 - 三种模式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-1-%E7%AE%80%E5%8D%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.2.1.</span> <span class="toc-text">3.2.1 简单工厂模式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-2-%E5%B7%A5%E5%8E%82%E6%96%B9%E6%B3%95%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.2.2.</span> <span class="toc-text">3.2.2 工厂方法模式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-3-%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.2.3.</span> <span class="toc-text">3.2.3 抽象工厂模式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E5%B0%8F%E7%BB%93"><span class="toc-number">4.3.</span> <span class="toc-text">3.3 - 工厂模式小结</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%8E%9F%E5%9E%8B%E6%A8%A1%E5%BC%8F"><span class="toc-number">5.</span> <span class="toc-text">四、原型模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E6%BA%90%E7%A0%81%E8%BF%BD%E6%BA%AF"><span class="toc-number">5.1.</span> <span class="toc-text">4.1 - 源码追溯</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">5.2.</span> <span class="toc-text">4.2 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3-%E6%B7%B1%E5%85%A5%E8%AE%A8%E8%AE%BA"><span class="toc-number">5.3.</span> <span class="toc-text">4.3 - 深入讨论</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-1-%E6%B5%85%E6%8B%B7%E8%B4%9D"><span class="toc-number">5.3.1.</span> <span class="toc-text">4.3.1 浅拷贝</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-2-%E6%B7%B1%E6%8B%B7%E8%B4%9D"><span class="toc-number">5.3.2.</span> <span class="toc-text">4.3.2 深拷贝</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%92%8C%E7%BB%86%E8%8A%82"><span class="toc-number">5.4.</span> <span class="toc-text">4.4 - 注意事项和细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%BB%BA%E9%80%A0%E8%80%85%E6%A8%A1%E5%BC%8F"><span class="toc-number">6.</span> <span class="toc-text">五、建造者模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">6.1.</span> <span class="toc-text">5.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2-%E5%BB%BA%E9%80%A0%E8%80%85%E6%A8%A1%E5%BC%8F%E7%9A%84%E5%9B%9B%E4%B8%AA%E8%A7%92%E8%89%B2"><span class="toc-number">6.2.</span> <span class="toc-text">5.2 - 建造者模式的四个角色</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-3-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%92%8C%E7%BB%86%E8%8A%82"><span class="toc-number">6.3.</span> <span class="toc-text">5.3 - 注意事项和细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E9%80%82%E9%85%8D%E5%99%A8%E6%A8%A1%E5%BC%8F"><span class="toc-number">7.</span> <span class="toc-text">六、适配器模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#6-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">7.1.</span> <span class="toc-text">6.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-2-%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86"><span class="toc-number">7.2.</span> <span class="toc-text">6.2 - 工作原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-3-%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="toc-number">7.3.</span> <span class="toc-text">6.3 - 三种方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#6-3-1-%E7%B1%BB%E9%80%82%E9%85%8D%E5%99%A8%E6%A8%A1%E5%BC%8F"><span class="toc-number">7.3.1.</span> <span class="toc-text">6.3.1 类适配器模式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-3-2-%E5%AF%B9%E8%B1%A1%E9%80%82%E9%85%8D%E5%99%A8%E6%A8%A1%E5%BC%8F"><span class="toc-number">7.3.2.</span> <span class="toc-text">6.3.2 对象适配器模式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-3-3-%E6%8E%A5%E5%8F%A3%E9%80%82%E9%85%8D%E5%99%A8%E6%A8%A1%E5%BC%8F"><span class="toc-number">7.3.3.</span> <span class="toc-text">6.3.3 接口适配器模式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-4-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%92%8C%E7%BB%86%E8%8A%82"><span class="toc-number">7.4.</span> <span class="toc-text">6.4 - 注意事项和细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%83%E3%80%81%E6%A1%A5%E6%8E%A5%E6%A8%A1%E5%BC%8F"><span class="toc-number">8.</span> <span class="toc-text">七、桥接模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#7-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">8.1.</span> <span class="toc-text">7.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">8.2.</span> <span class="toc-text">7.2 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AB%E3%80%81%E8%A3%85%E9%A5%B0%E8%80%85%E6%A8%A1%E5%BC%8F"><span class="toc-number">9.</span> <span class="toc-text">八、装饰者模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#8-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">9.1.</span> <span class="toc-text">8.1 - 基本介绍</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B9%9D%E3%80%81%E7%BB%84%E5%90%88%E6%A8%A1%E5%BC%8F"><span class="toc-number">10.</span> <span class="toc-text">九、组合模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#9-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">10.1.</span> <span class="toc-text">9.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">10.2.</span> <span class="toc-text">9.2 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E3%80%81%E5%A4%96%E8%A7%82%E6%A8%A1%E5%BC%8F"><span class="toc-number">11.</span> <span class="toc-text">十、外观模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#10-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">11.1.</span> <span class="toc-text">10.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%92%8C%E7%BB%86%E8%8A%82"><span class="toc-number">11.2.</span> <span class="toc-text">10.2 - 注意事项和细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E4%B8%80%E3%80%81%E4%BA%AB%E5%85%83%E6%A8%A1%E5%BC%8F"><span class="toc-number">12.</span> <span class="toc-text">十一、享元模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#11-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">12.1.</span> <span class="toc-text">11.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-2-%E5%86%85%E9%83%A8%E7%8A%B6%E6%80%81%E5%92%8C%E5%A4%96%E9%83%A8%E7%8A%B6%E6%80%81"><span class="toc-number">12.2.</span> <span class="toc-text">11.2 - 内部状态和外部状态</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-3-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">12.3.</span> <span class="toc-text">11.3 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E4%BA%8C%E3%80%81%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F"><span class="toc-number">13.</span> <span class="toc-text">十二、代理模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#12-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">13.1.</span> <span class="toc-text">12.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-2-%E5%87%A0%E7%A7%8D%E6%A8%A1%E5%BC%8F"><span class="toc-number">13.2.</span> <span class="toc-text">12.2 - 几种模式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#12-2-1-%E9%9D%99%E6%80%81%E4%BB%A3%E7%90%86"><span class="toc-number">13.2.1.</span> <span class="toc-text">12.2.1 静态代理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#12-2-2-%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86"><span class="toc-number">13.2.2.</span> <span class="toc-text">12.2.2 动态代理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#12-2-3-Cglib%E4%BB%A3%E7%90%86"><span class="toc-number">13.2.3.</span> <span class="toc-text">12.2.3 Cglib代理</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E4%B8%89%E3%80%81%E6%A8%A1%E6%9D%BF%E6%96%B9%E6%B3%95%E6%A8%A1%E5%BC%8F"><span class="toc-number">14.</span> <span class="toc-text">十三、模板方法模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#13-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">14.1.</span> <span class="toc-text">13.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13-2-%E9%92%A9%E5%AD%90%E6%96%B9%E6%B3%95"><span class="toc-number">14.2.</span> <span class="toc-text">13.2 - 钩子方法</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E5%9B%9B%E3%80%81%E5%91%BD%E4%BB%A4%E6%A8%A1%E5%BC%8F"><span class="toc-number">15.</span> <span class="toc-text">十四、命令模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#14-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">15.1.</span> <span class="toc-text">14.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">15.2.</span> <span class="toc-text">14.2 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E4%BA%94%E3%80%81%E8%AE%BF%E9%97%AE%E8%80%85%E6%A8%A1%E5%BC%8F"><span class="toc-number">16.</span> <span class="toc-text">十五、访问者模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#15-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">16.1.</span> <span class="toc-text">15.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#15-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">16.2.</span> <span class="toc-text">15.2 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E5%85%AD%E3%80%81%E8%BF%AD%E4%BB%A3%E5%99%A8%E6%A8%A1%E5%BC%8F"><span class="toc-number">17.</span> <span class="toc-text">十六、迭代器模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#16-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">17.1.</span> <span class="toc-text">16.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#16-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">17.2.</span> <span class="toc-text">16.2 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E4%B8%83%E3%80%81%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F"><span class="toc-number">18.</span> <span class="toc-text">十七、观察者模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#17-1-%E5%8E%9F%E7%90%86"><span class="toc-number">18.1.</span> <span class="toc-text">17.1 - 原理</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E5%85%AB%E3%80%81%E4%B8%AD%E4%BB%8B%E8%80%85%E6%A8%A1%E5%BC%8F"><span class="toc-number">19.</span> <span class="toc-text">十八、中介者模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#18-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">19.1.</span> <span class="toc-text">18.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#18-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">19.2.</span> <span class="toc-text">18.2 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E4%B9%9D%E3%80%81%E5%A4%87%E5%BF%98%E5%BD%95%E6%A8%A1%E5%BC%8F"><span class="toc-number">20.</span> <span class="toc-text">十九、备忘录模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#19-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">20.1.</span> <span class="toc-text">19.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#19-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">20.2.</span> <span class="toc-text">19.2 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E5%8D%81%E3%80%81%E8%A7%A3%E9%87%8A%E5%99%A8%E6%A8%A1%E5%BC%8F"><span class="toc-number">21.</span> <span class="toc-text">二十、解释器模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#20-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">21.1.</span> <span class="toc-text">20.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#20-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">21.2.</span> <span class="toc-text">20.2 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E5%8D%81%E4%B8%80%E3%80%81%E7%8A%B6%E6%80%81%E6%A8%A1%E5%BC%8F"><span class="toc-number">22.</span> <span class="toc-text">二十一、状态模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#21-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">22.1.</span> <span class="toc-text">21.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#21-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">22.2.</span> <span class="toc-text">21.2 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E5%8D%81%E4%BA%8C%E3%80%81%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F"><span class="toc-number">23.</span> <span class="toc-text">二十二、策略模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#22-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">23.1.</span> <span class="toc-text">22.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#22-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">23.2.</span> <span class="toc-text">22.2 - 注意事项及细节</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E5%8D%81%E4%B8%89%E3%80%81%E8%81%8C%E8%B4%A3%E9%93%BE%E6%A8%A1%E5%BC%8F"><span class="toc-number">24.</span> <span class="toc-text">二十三、职责链模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#23-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-number">24.1.</span> <span class="toc-text">23.1 - 基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#23-2-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E5%8F%8A%E7%BB%86%E8%8A%82"><span class="toc-number">24.2.</span> <span class="toc-text">23.2 - 注意事项及细节</span></a></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="footer_custom_text">却道天凉好个秋</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="Increase font size"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="Decrease font size"><i class="fas fa-minus"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      false && mermaid.init()
    })
  }
}</script></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>